Methods for adding columns and rows to a asp.net datatable
Source: Internet
Author: User
methods for adding columns and rows to a asp.net DataTableMethod One:
DataTable Tbldatas = new DataTable ("Datas");
DataColumn DC = null;
DC = TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32"));
dc. AutoIncrement = true;//automatically increased
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);
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 a table
DataColumn pricecolumn = new DataColumn ();
The data type of the column
Pricecolumn.datatype = System.Type.GetType ("System.Decimal");
The name of the list
Pricecolumn.columnname = "Price";
Default value for this column
Pricecolumn.defaultvalue = 50;
Create 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 values 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 the table
Table. Columns.Add (Pricecolumn);
Table. Columns.Add (Taxcolumn);
Table. Columns.Add (Totalcolumn);
Create a row
DataRow row = table. NewRow ();
Add this row to the table
Table. Rows.Add (row);
Put a table in an attempt
DataView view = new DataView (table);
Dg. DataSource = view;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.