Share via


DataColumn.Expression Properti

Definisi

Mendapatkan atau mengatur ekspresi yang digunakan untuk memfilter baris, menghitung nilai dalam kolom, atau membuat kolom agregat.

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

Nilai Properti

Ekspresi untuk menghitung nilai kolom, atau membuat kolom agregat. Jenis pengembalian ekspresi ditentukan oleh DataType kolom .

Atribut

Pengecualian

Properti AutoIncrement atau Unique diatur ke true.

Saat Anda menggunakan fungsi CONVERT, ekspresi mengevaluasi ke string, tetapi string tidak berisi representasi yang dapat dikonversi ke parameter jenis.

Ketika Anda menggunakan fungsi CONVERT, pemeran yang diminta tidak dimungkinkan. Lihat fungsi Konversi di bagian berikut untuk informasi terperinci tentang kemungkinan transmisi.

Saat Anda menggunakan fungsi SUBSTRING, argumen mulai berada di luar rentang.

-Atau-

Saat Anda menggunakan fungsi SUBSTRING, argumen panjang berada di luar rentang.

Saat Anda menggunakan fungsi LEN atau fungsi TRIM, ekspresi tidak mengevaluasi ke string. Ini termasuk ekspresi yang mengevaluasi ke Char.

Contoh

Contoh berikut membuat tiga kolom dalam DataTable. Kolom kedua dan ketiga berisi ekspresi; yang kedua menghitung pajak menggunakan tarif pajak variabel, dan yang ketiga menambahkan hasil perhitungan ke nilai kolom pertama. Tabel yang dihasilkan ditampilkan dalam DataGrid kontrol.

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

Keterangan

Untuk informasi selengkapnya tentang API ini, lihat Keterangan API Tambahan untuk DataColumn.Expression.

Berlaku untuk

Lihat juga