Asp.net datatable: how to add columns and rows

Source: Internet
Author: User

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

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

Method 3:
Datatable table = new datatable ();

// Create the first column of the table
Datacolumn pricecolumn = new datacolumn ();
// Data type of the column
Pricecolumn. datatype = system. type. GetType ("system. Decimal ");
// Name of the column
Pricecolumn. columnname = "price ";
// Obtain the default value for this column
Pricecolumn. defaultvalue = 50;

// Create the second column of the table
Datacolumn taxcolumn = new datacolumn ();
Taxcolumn. datatype = system. type. GetType ("system. Decimal ");
// Column name
Taxcolumn. columnname = "tax ";
// Set the column expression to calculate the value in the column or create an aggregate Column
Taxcolumn. Expression = "price * 0.0862 ";
// Create third column.
Datacolumn totalcolumn = new datacolumn ();
Totalcolumn. datatype = system. type. GetType ("system. Decimal ");
Totalcolumn. columnname = "Total ";
// Expression of this column. The value is the sum of the values of the first and second columns.
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 table
Table. Rows. Add (ROW );

// Put the table in an attempt
Dataview view = new dataview (table );
DG. datasource = view;

DG. databind ();

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.