Copy code code as follows:
#region Method one:
DataTable tbldatas = new DataTable ("Datas");
DataColumn dc = null;
DC = tblDatas.Columns.Add ("ID", Type.GetType ("System.Int32"));
DC. AutoIncrement = true;//automatically increases the
DC. AutoIncrementSeed = 1;//starting at 1
DC. 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 lying";
TblDatas.Rows.Add (NewRow);
#endregion
Copy Code code as follows:
#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
Copy Code code as follows:
#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;//The 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";//Set the expression to calculate the value in the column or 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
DataRow row = table. NewRow ();
Table. Rows.Add (row);//Add this row to the table
Put a table in an attempt
DataView view = new DataView (table);
Bind to DataGrid
Dg. DataSource = view;
Dg. DataBind ();
#endregion