Datatable 列expression使用

来源:互联网 发布:网络热词及解释 编辑:程序博客网 时间:2024/06/05 19:10

具体可查询MSDN:http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.expression(v=vs.80).aspx

http://www.cnblogs.com/XGLSummer/archive/2012/09/03/2668344.html

http://www.360doc.com/content/09/0611/11/15822_3853228.shtml

http://hi.baidu.com/a250152/item/ddf87cf54f1aaa0885d27876

应用如下所示:

View Code
 private void CalcColumns()
 {
     DataTable table = new DataTable ();
 
     // Create the first column.
     DataColumn priceColumn = new DataColumn();
     priceColumn.DataType = System.Type.GetType("System.Decimal");
     priceColumn.ColumnName = "price";
     priceColumn.DefaultValue = 50;
     
     // Create the second, calculated, column.
     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";
 
     // Add columns to DataTable.
     table.Columns.Add(priceColumn);
     table.Columns.Add(taxColumn);
     table.Columns.Add(totalColumn);
 
     DataRow row = table.NewRow();
     table.Rows.Add(row);
     DataView view = new DataView(table);
     dataGrid1.DataSource = view;
 }

 

或者:  dataDt.Columns.Add(“colName”, System.Type.GetType("System.String"),item.Expression);

注意:对于expression如下形式Col1+Col2,如果Col1为null,则新列也为null,需用IsNull(Col1,'')函数转化下

原创粉丝点击