C#datatableの作成

13130 ワード

Asp.net DataTable        

   :



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);



   :



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" });



   :

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";

// Create third column.

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 

table.Rows.Add(row);



// table     

DataView view = new DataView(table);

dg.DataSource = view;



dg.DataBind();