DataTableに行、列を追加

3600 ワード

DataTableへの列の追加
          
  //     
            DataColumn dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "isException";

datatable行を追加して最初の行に配置
DataTable dt = new DataTable();

            dt = db.reDt(sqlstr);

            //      
            DataRow dr = dt.NewRow();
            dr["apll_id"] = 0 ;
            dr["apll_name"] = "   ";
	 //        
            dt.Rows.InsertAt(dr,0);

方法1
DataTable tblDatas = new DataTable("Datas"); 

DataColumn dc = null; 
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); 
dc.AutoIncrement = true;//     
dc.AutoIncrementSeed = 1;//   1 
dc.AutoIncrementStep = 1;//   1 
dc.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"] = "    "; 
newRow["Version"] = "2.0"; 
newRow["Description"] = "    "; 
tblDatas.Rows.Add(newRow); 

newRow = tblDatas.NewRow(); 
newRow["Product"] = "    "; 
newRow["Version"] = "3.0"; 
newRow["Description"] = "      "; 
tblDatas.Rows.Add(newRow); 

方法2:
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" }); 

方法3
DataTable table = new DataTable(); 

//  table     
DataColumn priceColumn = new DataColumn(); 
priceColumn.DataType = System.Type.GetType("System.Decimal");//        
priceColumn.ColumnName = "price";//      
priceColumn.DefaultValue = 50;//       

//   table     
DataColumn taxColumn = new DataColumn(); 
taxColumn.DataType = System.Type.GetType("System.Decimal"); 
taxColumn.ColumnName = "tax";//   
taxColumn.Expression = "price * 0.0862";//        ,               

//   table     
DataColumn totalColumn = new DataColumn(); 
totalColumn.DataType = System.Type.GetType("System.Decimal"); 
totalColumn.ColumnName = "total"; 
totalColumn.Expression = "price + tax";//      ,            

//         table  
table.Columns.Add(priceColumn); 
table.Columns.Add(taxColumn); 
table.Columns.Add(totalColumn); 

//     
DataRow row = table.NewRow(); 
table.Rows.Add(row);//      table  

// table      
DataView view = new DataView(table); 

//   DataGrid 
dg.DataSource = view; 
dg.DataBind();