DataColumn.Expression 屬性

定義

取得或設定運算式,用來篩選資料列、計算資料行中的值或建立彙總資料行。

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, set
Public Property Expression As String

屬性值

運算式,用來計算資料行的值或建立彙總資料行。 運算式的傳回型別是由資料行的 DataType 所判斷。

屬性

例外狀況

AutoIncrementUnique 屬性是設定為 true

在使用 CONVERT 函式時,運算式評估為字串,但是字串不包含可以轉變為型別參數的表示。

在使用 CONVERT 函式時,不可能進行所要求的轉型。 請參閱下面段落中的型別轉換函式 (Conversion Function),取得有關可能轉型的詳細資訊。

在使用 SUBSTRING 函式時,起始引數超出範圍。

-或-

在使用 SUBSTRING 函式時,長度引數超出範圍。

在使用 LEN 函式或 TRIM 函式時,運算式不會評估為字串。 這包括評估為 Char 的運算式。

範例

下列範例會在 中建立三個 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 備註

適用於

另請參閱