three ways to add columns and rows to a DataTable#region Method One:
DataTable Tbldatas = new DataTable ("Datas");
DataColumn DC = null;
DC = TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32"));
dc. AutoIncrement = true; Automatically increase
dc. AutoIncrementSeed = 1; Starting at 1
dc. AutoIncrementStep = 1; Step 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" = "This place is the value of the cell";
newrow["Version"] = "2.0";
newrow["Description"] = "This place is the value of the cell";
TBLDATAS.ROWS.ADD (NewRow);
NewRow = Tbldatas.newrow ();
newrow["Product" = "This place is the value of the cell";
newrow["Version"] = "3.0";
newrow["Description"] = "This place is the value of the cell";
TBLDATAS.ROWS.ADD (NewRow);
#endregion
#region 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"});
#endregion
#region Method Three:
DataTable table = new DataTable ();
Create the first column of a table
DataColumn pricecolumn = new DataColumn ();
Pricecolumn.datatype = System.Type.GetType ("System.Decimal"); The data type of the column
Pricecolumn.columnname = "Price"; The name of the list
Pricecolumn.defaultvalue = 50; Default value for this column
Create second column of table
DataColumn taxcolumn = new DataColumn ();
Taxcolumn.datatype = System.Type.GetType ("System.Decimal");
Taxcolumn.columnname = "tax"; Column Name
Taxcolumn.expression = "Price * 0.0862"; Sets the expression that is used to calculate values in a column or to create an aggregation column
Create third column of table
DataColumn totalcolumn = new DataColumn ();
Totalcolumn.datatype = System.Type.GetType ("System.Decimal");
Totalcolumn.columnname = "Total";
Totalcolumn.expression = "Price + tax"; The expression of the column is the first column and the second column worth and
Add all columns to the table
Table. Columns.Add (Pricecolumn);
Table. Columns.Add (Taxcolumn);
Table. Columns.Add (Totalcolumn);
Create a row