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