DataColumn.Expression 属性  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置表达式,用于筛选行、计算列中的值或创建聚合列。
public:
 property System::String ^ Expression { System::String ^ get(); void set(System::String ^ value); };public string Expression { get; set; }[System.Data.DataSysDescription("DataColumnExpressionDescr")]
public string Expression { get; set; }member this.Expression : string with get, set[<System.Data.DataSysDescription("DataColumnExpressionDescr")>]
member this.Expression : string with get, setPublic Property Expression As String属性值
用来计算列的值,或创建聚合列的表达式。 表达式的返回类型由列的 DataType 来确定。
- 属性
例外
将 AutoIncrement 或 Unique 属性设置为 true。
在使用 CONVERT 函数时,表达式的计算结果为一个字符串,但该字符串不包含可以转换成类型参数的表示形式。
在使用 CONVERT 函数时,所请求的强制转换不可能完成。 有关可能的强制转换的详细信息,请参见下面一节中的 Conversion 函数。
示例
以下示例在 中创建三列 DataTable。 第二列和第三列包含表达式:第二个使用可变税率计算税收,第三个将计算结果添加到第一列的值。 生成的表显示在 控件 DataGrid 中。
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;
}
Private Sub CalcColumns()
     Dim rate As Single = .0862
     Dim table As New DataTable()
 
     ' Create the first column.
     Dim priceColumn As New DataColumn()
     With priceColumn
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "price"
         .DefaultValue = 50
     End With
     
     ' Create the second, calculated, column.
     Dim taxColumn As New DataColumn()
     With taxColumn
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "tax"
         .Expression = "price * 0.0862"
     End With
     
    ' Create third column
     Dim totalColumn As New DataColumn()
     With totalColumn
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "total"
         .Expression = "price + tax"
     End With
 
     ' Add columns to DataTable
     With table.Columns
         .Add(priceColumn)
         .Add(taxColumn)
         .Add(totalColumn)
     End With
    
     Dim row As DataRow= table.NewRow
     table.Rows.Add(row)
     Dim view As New DataView
     view.Table = table
     DataGrid1.DataSource = view
 End Sub
注解
有关此 API 的详细信息,请参阅 DataColumn.Expression 的补充 API 说明。