Summarization of C # assembling Datatable3 method

Source: Internet
Author: User


Methods for adding columns and rows to a asp.net datatable

Method One:

Datatable tbldatas = new datatable ("Datas");
datacolumn dc = null;
Dc = tbldatas.columns.add ("ID",  type.gettype ("System.Int32")); dc. autoincrement = true;//automatically increases DC. The autoincrementseed = 1;//starts with a 1 DC. The 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 the boast"; TBLDATAS.ROWS.ADD (NewRow);

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"});

Method Three:

datatable table = new datatable  ();
Create the first column of Table Datacolumn pricecolumn = new datacolumn ();
The data type of the column is Pricecolumn.datatype = system.type.gettype ("System.Decimal");
The name pricecolumn.columnname =  "Price";
The default value for this column pricecolumn.defaultvalue = 50;
  Create a 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 the value 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 table on table.
Columns.Add (Pricecolumn); Table.
Columns.Add (Taxcolumn); Table.Columns.Add (Totalcolumn); Create a row of datarow row = table.
NewRow (); Adds the row to table in table.
Rows.Add (row);
Place the table in an attempt to dataview view = new dataview (table); Dg.
datasource = view; Dg. DataBind ();

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.