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 によって決定されます。

属性

例外

AutoIncrement プロパティまたは Unique プロパティが true に設定されます。

CONVERT 関数を使用すると式が文字列と評価されますが、その文字列には型パラメーターに変換できる形式は含まれていません。

CONVERT 関数を使用する場合は、要求したキャストを実行できません。 実行できるキャストの詳細については、以降のセクションで変換関数に関するトピックを参照してください。

SUBSTRING 関数を使用する場合は、開始引数が範囲外にあります。

または

SUBSTRING 関数を使用する場合は、長さ引数が範囲外にあります。

LEN 関数または TRIM 関数を使用する場合は、式が文字列と評価されません。 これには Char と評価される式も含まれます。

次の例では、 に 3 つの列を DataTable作成します。 2 番目と 3 番目の列には式が含まれています。2 つ目は、可変税率を使用して税を計算し、3 番目は計算結果を最初の列の値に追加します。 結果のテーブルがコントロールに 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 解説」を参照してください。

適用対象

こちらもご覧ください