DataColumn.Expression Właściwość

Definicja

Pobiera lub ustawia wyrażenie używane do filtrowania wierszy, obliczania wartości w kolumnie lub tworzenia kolumny agregującej.

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

Wartość właściwości

Wyrażenie do obliczenia wartości kolumny lub utworzenie kolumny agregującej. Zwracany typ wyrażenia jest określany przez DataType kolumnę .

Atrybuty

Wyjątki

Właściwość or Unique jest ustawiona AutoIncrement na truewartość .

Gdy używasz funkcji CONVERT, wyrażenie oblicza ciąg, ale ciąg nie zawiera reprezentacji, którą można przekonwertować na parametr typu.

Jeśli używasz funkcji CONVERT, żądane rzutowanie nie jest możliwe. Szczegółowe informacje na temat możliwych rzutów można znaleźć w funkcji Conversion w poniższej sekcji.

Jeśli używasz funkcji SUBSTRING, argument początkowy jest poza zakresem.

— Lub —

Jeśli używasz funkcji SUBSTRING, argument length jest poza zakresem.

Jeśli używasz funkcji LEN lub funkcji TRIM, wyrażenie nie daje wartości ciągu. Obejmuje to wyrażenia, które są obliczane na wartość Char.

Przykłady

Poniższy przykład tworzy trzy kolumny w obiekcie DataTable. Druga i trzecia kolumna zawiera wyrażenia; Drugi oblicza podatek przy użyciu zmiennej stawki podatkowej, a trzeci dodaje wynik obliczenia do wartości pierwszej kolumny. Wynikowa tabela jest wyświetlana w kontrolce 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

Uwagi

Aby uzyskać więcej informacji na temat tego interfejsu API, zobacz dodatkowe uwagi dotyczące interfejsu API dla elementu DataColumn.Expression.

Dotyczy

Zobacz też