


| |
The Product Form
The form contains a tablegrid and Add, Delete, Sane and
Refresh buttons to perform record operations.

The form code is placed in its own file called ProductSetup.cs
and uses the same namespace as the Main.cs and MainWindow.cs files. The
class is initiated given the vbox and ODBC Connection from the MainWindow.
Passing the vbox allows the MainWindow code to control the value of vbox.
It also allows MainWindow to establish the ODBC connection. Finally, in order to
reduce code for the three forms, some of the code was placed in a separate class
called GridEvents.
Since the GridEvents code is common, the SelectCommand sets
the fields for the initial form display and the Refresh button. The
UpdateCommand sets the field(s) to be used as the identifier for database
modifications. In this case, setting the value to one (1) means the the Save
button will use the "prod_id" identifier.
using Gtk;
using Gdk;
using System.Data;
using System.Data.Odbc;
using EntityLIB;
using TableGridLIB;
namespace Inventory
{
public class ProductSetup
{
public TableGrid TG = new TableGrid( 10, 2 );
public GridEvents ge = null;
//
Color BlueColor = new Gdk.Color( 224, 255, 255 );
string fields_product = "prod_id,prod_desc";
string where_product = "";
string sort_product = "prod_id ASC";
public ProductSetup( VBox vbox, OdbcConnection connect )
{
ge = new GridEvents( vbox, "product",
TG, connect );
display( vbox );
ge.SelectCommand( fields_product,
where_product, sort_product );
int[] key = new int[1];
key[0] = 1;
ge.UpdateCommand( key );
ge.GetData();
}
void display( VBox vbox ) {
vbox.PackStart( TG.Grid, false,
false, 5 );
vbox.PackEnd( TG.GridPager, false,
false, 5 );
TG.hasAssistMenu = false;
TG.hasSelectMenu = false;
TG.hasGridColorAlternate = true;
TG.GridColorAlternate = BlueColor;
TG.DrawDatasheet();
TG.SetColumnHeader( 0, "Product" );
TG.SetColumnHeader( 1, "Description"
);
TG.SetColumnWidth( 1, 15 );
TG.SetColumnWidth( 2, 30 );
}
} //class
} //namespace
The GridEvent code is described in the Common.cs file.
The class is initiated with the same vbox passed to the Product form (or to one
of the other forms). The class code provides the functionality for the
Add, Delete, Save and Refresh buttons. A custom event, MessageReady, is
setup to display ShowMsg messages. Finally, there is the AskDialog method
to prompt the user.
using System;
using Gtk;
using Gdk;
using System.Data;
using System.Data.Odbc;
using EntityLIB;
using TableGridLIB;
namespace Inventory
{
public class GridEvents
{
public event MessageHandler MessageReady;
public Button AddBtn = null;
public Button DeleteBtn = null;
public Button SaveBtn = null;
public Button RefreshBtn = null;
//
public TableGrid TG = null;
public DataSet DS = null;
public int Rowcount = 0;
//
HBox ActBox = null;
Entity entity1 = null;
string fields = "";
string where = "";
string sort = "";
int[] key = null;
public GridEvents( VBox vbox, string table, TableGrid tg,
OdbcConnection connect )
{
TG = tg;
entity1 = new Entity( connect, table,
"" );
setup( vbox );
control();
}
//*** Setup methods
**********************************************
void setup( VBox vbox ) {
AddBtn = new Gtk.Button( Stock.Add );
DeleteBtn = new Gtk.Button(
Stock.Delete );
SaveBtn = new Gtk.Button( Stock.Save
);
RefreshBtn = new Gtk.Button(
Stock.Refresh );
ActBox = new Gtk.HBox();
ActBox.PackStart( AddBtn, false,
false, 5 );
ActBox.PackStart( DeleteBtn, false,
false, 5 );
ActBox.PackStart( SaveBtn, false,
false, 5 );
ActBox.PackStart( RefreshBtn, false,
false, 5 );
vbox.PackEnd( ActBox, false, false, 5
);
vbox.ShowAll();
} //setup_control
void control() {
AddBtn.Clicked += new
System.EventHandler( OnAddRow );
DeleteBtn.Clicked += new
System.EventHandler( OnDeleteRow );
SaveBtn.Clicked += new
System.EventHandler( OnSaveTable );
RefreshBtn.Clicked += new
System.EventHandler( OnRefresh );
TG.RowReadyForDelete += new
TableGridLIB.DeleteHandler( OnDelete );
TG.RowReadyForInsert += new
TableGridLIB.InsertHandler( OnInsert );
TG.RowReadyForUpdate += new
TableGridLIB.UpdateHandler( OnUpdate );
}
//*** Command methods
**********************************************
public void SelectCommand( string Fields, string Where,
string Sort ) {
fields = Fields;
where = Where;
sort = Sort;
}
public void UpdateCommand( int[] Key ) {
key = Key;
}
public void GetData() {
DS = new DataSet();
try {
DS =
entity1.GetDataSet( fields, where, sort );
Rowcount =
DS.Tables[0].Rows.Count;
TG.ReadDS( DS
);
TG.FirstPage();
} catch ( System.Exception ex ) {
ShowMsg(
"Table connect error: " + ex.Message );
Console.WriteLine( "Table connect error: " + ex.Message );
}
}
//*** Button methods
**********************************************
private void OnRefresh(object sender, System.EventArgs e)
{
DoRefresh();
}
private void DoRefresh()
{
GetData();
ShowMsg( "Refresh loaded rows: " +
Rowcount );
}
private void OnSaveTable(object sender, System.EventArgs e)
{
TG.ModifyTable( key, DS );
}
void OnAddRow(object sender, System.EventArgs e)
{
TG.AddRow();
Rowcount += 1;
ShowMsg( " Row added, modify and
click the 'Save' button. " );
} //AddRow
void OnDeleteRow(object sender, System.EventArgs e)
{
string selected_row = TG.GetCell(
TG.SelectedRow, 1 );
bool response = false;
response = AskDialog( "Do you really
want to delete: " + selected_row );
if ( response == true ) {
TG.DeleteRow(
TG.SelectedRow + TG.CurrentPageTopRow - 1 );
OnSaveTable(
sender, e );
Rowcount -=
1;
DoRefresh();
ShowMsg( "
Row deleted. " );
}
} //DeleteRow
//*** Entity Methods
*****************************************************************
void OnUpdate(string Table, string Fields, string Where ) {
entity1.UpdateRow( Fields, Where );
ShowMsg( Table + " Updated: " +
Fields + " for: " + Where );
}
void OnInsert(string Table, string Fields, string Values ) {
entity1.InsertRow( Fields, Values );
ShowMsg( "Insert: " + entity1.Note );
}
void OnDelete(string Table, string Where ) {
entity1.DeleteRow( Where );
ShowMsg( "Delete: " + entity1.Note );
}
//*** Misc Functions
***************************************************************************
void ShowMsg( string msg ) {
if ( MessageReady != null )
this.MessageReady( msg );
}
public bool AskDialog( string Message ) {
bool response = false;
MessageDialog md = new MessageDialog
(
null,
DialogFlags.DestroyWithParent,
MessageType.Question,
ButtonsType.YesNo, Message );
int nRc = md.Run ();
if (nRc == (int)ResponseType.Yes) {
response =
true;
md.Destroy();
} else {
response =
false;
md.Destroy();
}
return response;
} //AskDialog
} //class
} //namespace
|