asp DataTable에 열 및 행을 추가하는 세 가지 방법

3711 단어
 
  
#region :
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);
#endregion
 
  
#region :
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" });
#endregion
 
  
#region :
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();
#endregion

좋은 웹페이지 즐겨찾기