ASP datatable three methods for adding columns and rows

Source: Internet
Author: User

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

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.