Three ways to add columns and rows to a DataTable

Source: Internet
Author: User

Jroger Original

#regionMethod One:DataTable Tbldatas=NewDataTable ("Datas");D atacolumn DC=NULL;d C= TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32") );d C. AutoIncrement=true;//automatically adddc. AutoIncrementSeed =1;//starting at 1dc. AutoIncrementStep =1;//Step size is 1dc. AllowDBNull =false;d C= TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String"));d C= TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String"));d C= TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String"));D Atarow 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

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

#regionMethod Three:DataTable Table=NewDataTable ();//Create the first column of a tableDataColumn Pricecolumn =NewDataColumn ();p Ricecolumn.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 ();#endregion

Three ways to add columns and rows to a DataTable

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.