1. 程式人生 > >DataTable新增列和行的三種方法

DataTable新增列和行的三種方法

#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);
//繫結到DataGriddg.DataSource = view;
dg.DataBind();
#endregion

本文轉自http://www.cnblogs.com/jRoger/articles/1887581.html