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