C # Create a DataTable

Source: Internet
Author: User

  c# Create DataTable Method One: datatable tbldatas = new datatable ("Datas");D atacolumn  Dc = null;dc = tbldatas.columns.add ("ID",  type.gettype ("System.Int32"));d C. autoincrement = true;//automatically increases the DC. The autoincrementseed = 1;//starts at 1DC. The autoincrementstep = 1;//step is 1DC. allowdbnull = false;//Dc = 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")); Datarow newrow;newrow = tbldatas.newrow (); newrow["Product"] =  "Big Talk Tour"; newRow["Version" ] =  "2.0"; newrow["Description"] =  "I love It"; TblDatas.Rows.Add (NewRow); Newrow = tbldatas.newrow (); newrow["Product"] =  "Fantasy Westward Tour" newrow["Version"] =  "3.0"; newrow["Description"] =  "more childish than big talk"; TblDatas.Rows.Add (NewRow); MethodII: 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 the table DatacoluMn pricecolumn = new datacolumn ();//data type of the column pricecolumn.datatype =  System.Type.GetType ("System.Decimal");//The name of the column pricecolumn.columnname =  "price";// This column has a default value of 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";//Set the column to be an expression, Used to calculate values in a column or to 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";//The column is an expression, 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); Creates a row of datarow row = table. NewRow ();//Adds this line to the table in table. Rows.Add(row); Place the table in an attempt to dataview view = new dataview (table);d G. datasource = view; Dg. DataBind ();

C # Create a DataTable (go)

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.