ADO+ also allows you to create and define expression columns. Expressions in ADO+ are used for:
To create an expression column, set the DataType property to a type appropriate to the values the expression will return; then set the Expression property to a valid expression:
[VB] Dim dc As DataColumn = New DataColumn dc.DataType = System.Type.GetType("System.Currency") dc.Expression = "total * .086" [C#] DataColumn dc = New DataColumn; dc.DataType = System.Type.GetType("System.Currency"); dc.Expression = "total * .086";
You can also create an expression column using an Add method. For example, the code below adds a column that calculates the rebate due to a customer based on 10% of their purchases. The expression multiplies the column named "Purchases" column by 10%.
[VB] Dim workColumn As DataColumn workColumn = workTable.Columns.Add("Rebates", _ System.Type.GetType("System.Double"), "Purchases * .1" ) [C#] System.Data.DataColumn workColumn; workColumn = workTable.Columns.Add("Rebates", System.Type.GetType("System.Double"), "Purchases * .1");
When the table is populated, this column's value will be 10% of the value in the Purchases column.
DataSet expressions can reference other expression columns. However, an error will be thrown if a "circular reference" is established.
For rules on writing expressions, see the help topic for the DataColumn class's Expression property.
Another feature of the DataColumn is its ability to be an auto-incrementing column. An auto-incrementing column automatically increments the value held in the column when new rows are added. To create an auto-incrementing column, set the column's AutoIncrement property to true. Once set, the column with start from the value defined in the column's AutoIncrementSeed property. With each row added, the value of the AutoIncrement column will raise by the value held in the column's AutoIncrementStep property
For example, to create a column that starts incrementing from 200 in steps of 3:
[VB] workColumn = workTable.Columns.Add _ ("CustID", System.Type.GetType("System.Int32") workColumn.AutoIncrement = True workColumn.AutoIncrementSeed = 200 workColumn.AutoIncrementStep = 3 [C#] workColumn = workTable.Columns.Add("CustID", System.Type.GetType("System.Int32") ); workColumn.AutoIncrement = true; workColumn.AutoIncrementSeed = 200; workColumn.AutoIncrementStep = 3;
We can also set the column to be read-only by setting its ReadOnly property to true.