Three ways to add columns and rows to a DataTable

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

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.