DataColumn.Expression 屬性

定義

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

public string Expression { get; set; }
[System.Data.DataSysDescription("DataColumnExpressionDescr")]
public string Expression { get; set; }

屬性值

運算式,用來計算資料行的值或建立彙總資料行。 運算式的傳回型別是由資料行的 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;
}

備註

如需此 API 的詳細資訊,請參閱 DataColumn.Expression 的補充 API 備註

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱