DataColumn.Expression プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
行のフィルター処理、列の値の計算、集計列の作成に使用する式を取得または設定します。
public:
property System::String ^ Expression { System::String ^ get(); void set(System::String ^ value); };
[System.Data.DataSysDescription("DataColumnExpressionDescr")]
public string Expression { get; set; }
public string Expression { get; set; }
[<System.Data.DataSysDescription("DataColumnExpressionDescr")>]
member this.Expression : string with get, set
member this.Expression : string with get, set
Public Property Expression As String
プロパティ値
列の値を計算する式、または集計列を作成する式。 式の戻り値の型は、列の DataType によって決まります。
- 属性
例外
AutoIncrement または Unique プロパティは、trueに設定されます。
CONVERT 関数を使用している場合、式は文字列に評価されますが、文字列には型パラメーターに変換できる表現が含まれていません。
CONVERT 関数を使用している場合、要求されたキャストは実行できません。 可能なキャストの詳細については、次のセクションの Conversion 関数を参照してください。
SUBSTRING 関数を使用すると、開始引数が範囲外になります。
-Or-
SUBSTRING 関数を使用すると、長さの引数が範囲外になります。
例
次の例では、 DataTableに 3 つの列を作成します。 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 解説」を参照してください。