CopyCode The Code is as follows: # Region Method 1:
Datatable tbldatas = new datatable ("datas ");
Datacolumn Dc = NULL;
Dc = tbldatas. Columns. Add ("ID", type. GetType ("system. int32 "));
DC. autoincrement = true; // automatically add
DC. autoincrementseed = 1; // start with 1
DC. autoincrementstep = 1; // The 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"] = "westward journey ";
Newrow ["version"] = "2.0 ";
Newrow ["Description"] = "I like it ";
Tbldatas. Rows. Add (newrow );
Newrow = tbldatas. newrow ();
Newrow ["product"] = "Fantastic westward journey ";
Newrow ["version"] = "3.0 ";
Newrow ["Description"] = "more naive than big talk ";
Tbldatas. Rows. Add (newrow );
# EndregionCopy codeThe Code is as follows: # Region Method 2:
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 codeThe Code is as follows: # region method 3:
Datatable table = new datatable ();
// Create the first column of the table
Datacolumn pricecolumn = new datacolumn ();
Pricecolumn. datatype = system. type. GetType ("system. Decimal"); // data type of this column
Pricecolumn. columnname = "price"; // name of this column
Pricecolumn. defaultvalue = 50; // default value for this column
// Create the second column of the table
Datacolumn taxcolumn = new datacolumn ();
Taxcolumn. datatype = system. type. GetType ("system. Decimal ");
Taxcolumn. columnname = "tax"; // column name
Taxcolumn. Expression = "price * 0.0862"; // sets the column expression to calculate the value in the column or create an aggregate column.
// Create the third column of the table
Datacolumn totalcolumn = new datacolumn ();
Totalcolumn. datatype = system. type. GetType ("system. Decimal ");
Totalcolumn. columnname = "Total ";
Totalcolumn. Expression = "Price + Tax"; // expression of this column, which is the sum of values in the first and second columns
// 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 table
// Put the table in an attempt
Dataview view = new dataview (table );
// Bind to the DataGrid
DG. datasource = view;
DG. databind ();
# Endregion