DataTable Objtable = New DataTable ("Newbooks");
// Declare a variable to hold a datacolumn Object Datacolumn Objcolumn;
// define the columns (fields) within the table // the first is an AutoIncrement column to act as the key objColumn = objTable.Columns.Add ( "kBookKey", Type.GetType ( "System.Int32")); objColumn. Autoincrement = true; objcolumn.autoincrementseed = 1000; objcolumn.autoincrementstep = 10;
// next, the ISBN column which must be unique and not allow Null values objColumn = objTable.Columns.Add ( "ISBN", Type.GetType ( "System.String")); objColumn.AllowDBNull = false; objColumn.Unique = Objcolumn.maxlength = 10;
// now two more columns to hold general information objtable.columns.add ("system.string"); objtable.columns.add ("publicationdate", type.gettype ("System.DateTime "));
// add two columns to hold stock and order quantities, with default values of zero objColumn = objTable.Columns.Add ( "StockQty", Type.GetType ( "System.Int32")); objColumn.DefaultValue = 0; objColumn = objTable .Columns.add ("OrderedQty", Type.gettype ("System.Int32")); ObjColumn.defaultValue = 0;
// add a column containing an expression showing the quantity availability objColumn = objTable.Columns.Add ( "AvailableQty", Type.GetType ( "System.Int32")); objColumn.Expression = "[StockQty] - [OrderedQty]";
// Declare a variable to hold a datarow object DataRow ObjDataRow;
// Create a new datarow Object instance in this table/datarow = objtable.newrow ();
// and fill in the value of = "1234567800"; objdatarow ["title"] = "Professional Video Recorder Programming"; Objdatarow ["PublicationDate"] = New DateTime (2001, 03, 01); ObjdataroW ["Stockqty"] = 3956; Objdatarow ["OrderedQty"] = 450; ObjTable.Rows.Add (ObjDataRow); // repeat to add two more rows objDataRow = objtable.newrow (); objdatarow ["ISBN"] = " 1234567801 "; Objdatarow [" title "] =" Professional Wap Phone Programming "; Objdatarow [" PublicationDate "] = New DateTime (2001, 06, 01); ObjDataRow [" stockqty "] = 329; // Note - no" OrderedQty "provided so default value use objtable.rows.add (objDataro);
Objdatarow = objtable.newrow (); objdatarow ["ISBN"] = "1234567802"; objdatarow ["title"] = "Professional Radio Station Programming"; ObjDataRow ["publicationDate"] = New DateTime (2001, 04, 01); // NOTE - NO "stockqty" provided so default value use objdatarow "] = 1200; objtable.rows.add (objDATAROW);
// Assign The DataTable's defaultview object to the datagrid control dgrresult.datasource = objtable.defaultview; dgrresult.Database (); // and bind (display) The data