Methods for adding columns and rows to a asp.net datatable
Method One:
Datatable tbldatas = new datatable ("Datas");
datacolumn dc = null;
Dc = tbldatas.columns.add ("ID", type.gettype ("System.Int32")); dc. autoincrement = true;//automatically increases DC. The autoincrementseed = 1;//starts with a 1 DC. The autoincrementstep = 1;//step size is 1 DC. allowdbnull = false;//Dc = tbldatas.columns.add ("Product", type.gettype ("
System.String "));
Dc = tbldatas.columns.add ("Version", type.gettype ("System.String"));
Dc = tbldatas.columns.add ("Description", type.gettype ("System.String"));
datarow newrow;
Newrow = tbldatas.newrow ();
newrow["Product"] = "Dahua West Tour";
newrow["Version"] = "2.0";
newrow["Description"] = "I like it very much";
TBLDATAS.ROWS.ADD (NewRow);
Newrow = tbldatas.newrow ();
newrow["Product"] = "Dream West Tour";
newrow["Version"] = "3.0";
newrow["Description"] = "more childish than the boast"; TBLDATAS.ROWS.ADD (NewRow);
Method Two:
DataTable Tbldatas = new DataTable ("Datas");
TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32"));
Tbldatas.columns[0]. AutoIncrement = true;
Tbldatas.columns[0]. AutoIncrementSeed = 1;
Tbldatas.columns[0]. AutoIncrementStep = 1;
TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String"));
TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String"));
TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String"));
TBLDATAS.ROWS.ADD (New Object[]{null, "A", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
Method Three:
datatable table = new datatable ();
Create the first column of Table Datacolumn pricecolumn = new datacolumn ();
The data type of the column is Pricecolumn.datatype = system.type.gettype ("System.Decimal");
The name pricecolumn.columnname = "Price";
The default value for this column pricecolumn.defaultvalue = 50;
Create a second column of Table Datacolumn taxcolumn = new datacolumn ();
Taxcolumn.datatype = system.type.gettype ("System.Decimal");
Column name taxcolumn.columnname = "tax";
Sets the expression that is used to calculate the value in a column or to create an aggregation column taxcolumn.expression = "price * 0.0862";
create third column.
Datacolumn totalcolumn = new datacolumn ();
Totalcolumn.datatype = system.type.gettype ("System.Decimal");
totalcolumn.columnname = "Total";
The expression of the column, the value is obtained is the first column and the second column is worth and totalcolumn.expression = "Price + tax"; Add all columns to table on table.
Columns.Add (Pricecolumn); Table.
Columns.Add (Taxcolumn); Table.Columns.Add (Totalcolumn); Create a row of datarow row = table.
NewRow (); Adds the row to table in table.
Rows.Add (row);
Place the table in an attempt to dataview view = new dataview (table); Dg.
datasource = view; Dg. DataBind ();