C # DataTable Add rows and columns

Source: Internet
Author: User

Original address: http://blog.csdn.net/u013084746/article/details/53024266

Method One:

DataTable table =NewDataTable ();//Create the first column of a tableDataColumn Pricecolumn =NewDataColumn (); Pricecolumn.datatype= System.Type.GetType ("System.Decimal");//the data type of the columnPricecolumn.columnname =" Price";//the name of the columnPricecolumn.defaultvalue = -;//The default value for this column//Create the second column of a tableDataColumn Taxcolumn =NewDataColumn (); Taxcolumn.datatype= System.Type.GetType ("System.Decimal"); Taxcolumn.columnname=" Tax";//Column NameTaxcolumn.expression ="Price * 0.0862";//set the column to an expression that evaluates the values in a column or creates an aggregate column//Create the third column of a tableDataColumn Totalcolumn =NewDataColumn (); Totalcolumn.datatype= System.Type.GetType ("System.Decimal"); Totalcolumn.columnname=" Total"; Totalcolumn.expression="Price + Tax";//The column's expression is the first column and the second column worth and//Add all columns to the tabletable. Columns.Add (Pricecolumn); Table. Columns.Add (Taxcolumn); Table. Columns.Add (Totalcolumn); //Create a rowDataRow row =table. NewRow (); Table. Rows.Add (row);//Add this line to the table//Place the table in the viewDataView view =NewDataView (table);//bind to DataGridDg. DataSource =view; DG. DataBind (); 

Method Two:

DataTable Tbldatas =NewDataTable ("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 (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C"});

Method Three:

DataTable Tbldatas =NewDataTable ("Datas"); DataColumn DC=NULL; DC= TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32")); dc. AutoIncrement=true;//automatically adddc. AutoIncrementSeed =1;//starting at 1dc. AutoIncrementStep =1;//Step size is 1dc. 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);

C # DataTable Add rows and columns

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.