Get or set an expression for filtering the rows, the values in the calculation column or create a polymer column.
[Visual Basic]
Public property expression as string
[C #]
Public String Expression {Get; set;}
[C ]
PUBLIC: __PROPERTY STRING * GET_EXPRESSION ();
PUBLIC: __PROPERTY VOID SET_EXPRESSION (STRING *);
[Jscript]
PUBLIC FUNCTION GET Expression (): String;
Public Function SET Expression (String);
Attribute value
Used to calculate the value of the column, or create an expression of the polymer column. The return type of the expression is determined by the column DATATYPE.
abnormal
Exception Type Conditions ArgumentException Sets the AutoInCrement or UNIQUE property to True. Formatexception When using the Convert function, the calculation result of the expression is a string, but the string does not include a representation that can be converted into type parameters. InvalidCastException When using the Convert function, the requested forced conversion is impossible to complete. For more information on possible forced conversion, see the conversion function below. ArgumentOfRangeException When using the substring function, the starting parameter is outside the range. - or - When using the substring function, the length parameter is outside the range. Exception When using the Len function or a TRIM function, the calculation result of the expression is not a string. This includes the expression of CHAR as char.
Note
One use of the Expression property is to create a calculated column. For example, to calculate the tax value, you must multiply the unit price by the tax rate given in a given area. Due to different tax rates, it is impossible to put a single tax rate in columns; then use the Expression attribute to calculate this value, as shown in the Visual Basic code below:
DataSet1.Tables ("Products"). Column ("TAX"). Expression = "Unitprice * 0.086"
The second use is to create a polymeric column. Similar to the calculated value, polymerization is performed based on the entire row of DataTable. A simple example is a method for counting the number of rows returned to the concentration, which is how you might be used to count the number of transactions completed by a particular sales staff, as shown in the Visual Basic code below:
DataSet1.Tables ("Orders"). Column ("ORDERCOUNT"). Expression = "Count (OrderID)"
Expression syntax
When creating an expression, use the columnName property to reference columns. For example, if a column columnName is "Unitprice" (unit price), the other is "quantity", the expression will be:
"Unitprice * quantity"
When you create an expression for the filter, place the string in single quotes:
"Lastname = 'Jones'"
The following characters are special characters, as explained below, if they are used in column names, they must be escaped:
/ n (newline)
/ t (tab)
/ R (Carriage Return)
~
(
)
#
/
/
=
>
<
-
*
%
&
|
^
'
"
[
]
If the column name contains one of the characters above, the name must be enclosed in parentheses. For example, if you want to use the column called "Column #" in the expression, you should write "[Column #]":
Total * [column #]
Since the brackets are special characters, if it is an integral part of the column name, it is necessary to use a slash ("/") to escape the brackets. For example, the column named "Column []" is written:
Total * [column [/]]]
(Only the second middle bracket must escape.)
User-defined value
User-defined values can be used in an expression that compares the column value. The string value should be placed in single quotes. The date value should be placed in the pound symbol (#). For values, the decimal and scientific counting is allowed. E.g:
"Firstname = 'john'"
"Price <= 50.00"
Birthdate <# 1/31/82 # "
For columns containing enumeration values, enforce values to integer data types. E.g:
"Enumcolumn = 5"
Operator
Allow in series when using Boolean and NOT operators. Brackets can be used to combine clauses and mandatory priorities. The And operator takes precedence over other operators. E.g:
(LastName = 'smith' or lastname = 'Jones') And firstname = 'john'
When you create a comparison expression, the following operators are allowed:
<
>
<=
> =
<>
=
In
Like
The following arithmetic operators are also supported in the expression:
(Plus)
-(Less)
* (Multiplied)
/(except)
% (Modulus)
String operator
To connect a string, use characters. String compares whether to case case in case case is determined by the value of the CaseSensitive property of the DataSet class. However, this value can be rewritten with the CaseSensitive property of the DataTable class.
Wildcard
In the LIKE comparison, both * and% can be interchangeably as a wildcard. If the string in the Like clause contains * or%, these character applications are escaping them. If there is a middle bracket in the clause, the bracket character is used to escape (for example, [[[] or []]). At the beginning and end of the mode, or at the end of the mode, or the wildcard is allowed at the beginning of the mode. E.g:
"ItemName Like '* Product *'"
"ItemName Like '* Product'"
"Itemname like 'product *'"
The wildcard is not allowed in the middle of the string. For example, 'TE * XT' is not allowed.
Father / sub-relations reference
By adding a Parent in front of the column name, you can reference the parent table in the expression. For example, a column of a parent table named price.
By adding a child in front of the column name, you can reference the columns in the subthech in the expression. However, since the child relationship can return multiple lines, it must include references to sublining in the aggregation function. For example, SUM (Child.price) will be the sum of the columns named Price in the returned sub table. If a table has multiple sub-tables, the syntax is: Child (Relationname). For example, if a table has two sub-tables, their names are Customers and Orders, and the DataRelelation object is named Customers2RDERS, the reference will be:
AVG (Customers2RDERS) .quantity
polymerization
Support the following aggregate types:
Sum (seeking)
AVG (average)
MIN (minimum)
Max (max)
Count (count)
StDEV (statistical deviation)
VAR (statistical variance).
The polymerization is usually performed along the relationship. Create a polymeric expression by using one of the functions listed above and the subtraces detailed in the "Father / Sub Refrection Reference" above. E.g:
AVG (child.price)
AVG (Orders2details) .price) .price
Aggregation can also be performed on a single table. For example, if you want to create a summary of numbers in columns called "Price", use:
SUM (Price)
Note If you use a table to create aggregation, there will be no combination. Instead, all rows are displayed in columns.
If the table is not line, the aggregate function will return to the empty reference (Nothing in Visual Basic).
Data types can always be determined by checking the DataType properties of the columns. You can also use the CONVERT function to convert the data type as shown below.
Functions
Also supports the following functions:
Convert
Note Converts a given expression to the specified .NET Framework type. Syntax Convert (Expression, Type) Parameter Expression - Expression to be converted. The Type-value will be converted to the .NET Framework type.
For example: mydatacolumn.expression = "Convert (Total, 'System.Int32')
All conversions are valid, only the following cases: Boolean can only be converted to byte, sbyte, int16, int32, int64, uint16, uint32, uint64, string, and itself. CHAR can only be converted to INT32, UINT32, String and itself. DateTime can only be converted with String and itself. TimeSpan can only be converted to String and itself.
Len
Description Gets the length syntax of the character string, a character string to be calculated.
For example: mydatacolumn.expression = "len (itemname)"
ISNULL
Description Check the expression and returns an inspected expression or returns the replacement value. Syntax Isnull (Expression, ReplacementValue) Parameters Expression - The expression to check. ReplacementValue - Returns ReplacementValue if the expression is an empty reference (Nothing).
For example: mydatacolumn.expression = "ISNULL (Price, -1)"
Iif
Description obtains one of two values based on the result of the logical expression. Syntax IIF (EXPR, TRUEPART, FALSEPART) Parameter EXPR - the expression to be calculated. TruePart - The expression returned when the expression is true. FalsePart - The expression returned by the expression. For example: mydatacolumn.expression = "IIF (Total> 1000, 'Expensive', 'Dear')
TRIM
Description Removes all preamble and suffix space characters, such as / r, / n, / t, '' Syntax Trim (Expression) Parameters Expression - To tailor.
Substring
Description Gets starting from the specified point in the string, a sub-string with a specified length. Syntax Substring (Expression, Start, Length) Parameter Expression - Source string of sub-string. START - Specifies the integer of the start position of the sub-string. Length - Specifies the integer of the length of the sub-string.
For example: mydatacolumn.expression = "Substring (Phone, 7, 8)"
Pay attention to
The Expression property assigns an empty or empty string to reset this property. If a default value is set for the expression column, reset
After the Expression property, this default value gives all previously filled rows.
Example
[Visual Basic, C #, C ] The following example creates three columns in the DataTable. The second and third columns contain an expression; the second column uses variable tax rates to calculate the tax, and the third column adds the calculated result to the value of the first column. Result Table is displayed in the DataGrid control.
[Visual Basic]
Private sub cagccolumns ()
DIM CPRICE AS Datacolumn
DIM CTAX As Datacolumn
DIM CTOTAL AS DATACOLUMN
DIM RATE AS SINGLE
Rate = .0862
Dim t as dataatable = new data
'CREATE THE FIRST Column.
CPRICE = New Datacolumn
With cprice
.Dattype = system.type.gettype ("system.decimal")
.Columnname = "price"
.Defaultvalue = 50
End with
'Create The Second, Calculated, Column.
CTAX = New Datacolumn
WITH CTAX
.Dattype = system.type.gettype ("system.decimal")
.Columnname = "TAX"
.Expression = "price * 0.0862"
End with
'Create Third Column
CTotal = New Datacolumn
WITH CTOTAL
.Dattype = system.type.gettype ("system.decimal")
.Columnname = "total"
.Expression = "Price Tax"
End with
'Add Column to DataTable
With T.Columns
.Add (cprice)
.Add (ctax)
.Add (ctotal) end with
DIM R AS DATAROW
R = T.Newrow
T.ROWS.ADD (R)
DIM DVIEW AS New DataView
DVIEW.TABLE = T
DataGrid1.datasource = DVIEW
End Sub
[C #]
Private void Calccolumns () {
Datacolumn CPRICE;
Datacolumn CTAX;
Datacolumn ctotal;
DataTable myTable = new dataable ();
// CREATE The first column.
CPRICE = New Datacolumn ();
CPRICE.DATATYPE = system.type.gettype ("System.Decimal");
CPRICE.COLUMNNAME = "price";
CPRICE.DEFAULTVALUE = 50;
// Create The Second, Calculate, Column.
CTAX = New Datacolumn ();
CTax.DataType = System.Type.gettype ("System.Decimal");
CTax.columnname = "TAX";
CTax.Expression = "Price * 0.0862";
// Create Third Column.
CTOTAL = New Datacolumn ();
Ctotal.DataType = system.type.gettype ("System.Decimal");
Ctotal.columnname = "total";
CTotal.Expression = "Price Tax";
// Add columns to datatable.
myTable.columns.Add (CPRice);
MyTable.Columns.Add (CTAX);
MyTable.Columns.Add (CTOTAL);
DataRow myrow;
Myrow = myTable.newrow ();
MyTable.Rows.Add (MyRow);
DataView MyView = New DataView (MyTable);
DataGrid1.datasource = MyView;
}
[C ]
Private:
Void Calccolumns () {
Datacolumn * cprice;
Datacolumn * CTAX;
Datacolumn * ctotal;
DataTable * myTable = new data ();
// CREATE The first column.
CPRICE = New Datacolumn ();
CPRICE-> DATATYPE = System :: Type :: gettype (s "system.decimal");
CPRICE-> columnname = s "price";
CPRICE-> DefaultValue = __box (50);
// Create The Second, Calculate, Column.
CTAX = New Datacolumn ();
CTax-> DataType = system :: type :: gettype (s "system.decimal"); ctax-> columnname = s "TAX";
CTAX-> Expression = s "price * 0.0862";
// Create Third Column.
CTOTAL = New Datacolumn ();
Ctotal-> datatype = system :: type :: gettype (s "system.decimal");
Ctotal-> columnnname = s "total";
Ctotal-> Expression = s "Price Tax";
// Add columns to datatable.
MyTable-> column-> add (cprice);
MyTable-> Column-> Add (ctax);
MyTable-> Column-> Add (ctotal);
DataRow * Myrow;
Myrow = mytable-> newrow ();
MyTable-> rows-> add (myrow);
DataView * myview = new data;
DataGrid1-> DataSource = MyView;
}
[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.
Claim
Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series, .NET Framework Lite
See
Datacolumn | Datacolumn Member | System.Data Namespace