ASP DataTable Three ways to add columns and rows _ Practical Tips

Source: Internet
Author: User
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
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.