DataTable에 행, 열 추가

3601 단어 asp.net
DataTable에 열 추가
          
  //     
            DataColumn dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "isException";

데이터탭에 줄을 추가하고 첫 줄에 놓기
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(); 

좋은 웹페이지 즐겨찾기