[]
Data binding enables the C1ExpressionEditor control to use fields from a data source in expressions. When a data source is assigned to the editor, its available fields can be referenced as expression tokens, such as [Price] or [ProductName], and are available through IntelliSense
Expression results can be evaluated using either the Evaluate() or TryEvaluate() method.
The Evaluate() method evaluates the current expression and returns the result directly.
The TryEvaluate() method performs the evaluation by using a bool return value to indicate success or failure and provides the result through an out parameter.
This topic demonstrates how to bind ExpressionEditor to a DataTable, select a DataRow as the evaluation context, and evaluate an expression by using TryEvaluate().
The DataSource and ItemContext properties determine the data available for expression authoring and evaluation.
Property | Role in This Example |
|---|---|
Assigns the | |
Specifies the |
Note: ItemContext is explicitly configured in this example to select a specific row for evaluation. Whether it needs to be set explicitly depends on the type and context of the bound data source.
Create a DataTable containing the columns required for expression evaluation. The column names can then be referenced as expression tokens.
The following DataTable contains four columns and three rows with values. Its columns are available to ExpressionEditor as [ProductName], [Price], [Stock], and [Discount].
private DataTable GetDataTable()
{
DataTable dt = new DataTable("Products");
// Add columns — these become [FieldName] tokens in IntelliSense
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Price", typeof(double));
dt.Columns.Add("Stock", typeof(int));
dt.Columns.Add("Discount", typeof(double));
// Add rows — these are the values available for evaluation
dt.Rows.Add("Apple", 1.50, 200, 0.10);
dt.Rows.Add("Banana", 0.75, 500, 0.05);
dt.Rows.Add("Cherry", 3.00, 150, 0.20);
return dt;
}Assign the DataTable to the DataSource property to populate IntelliSense with the available column names.
Configure the data source during form initialization or load.
DataTable dt = GetDataTable();
// Give the DataTable to the editor
// The editor reads column names: ProductName, Price, Stock, Discount
// These appear as [ProductName], [Price], [Stock], [Discount] in IntelliSense
editor.DataSource = dt;Assign a DataRow to the ItemContext property to specify which row supplies the values used during expression evaluation.
In the following example, the first row represents Apple, with a price of 1.50, stock of 200, and discount of 0.10.
// dt.Rows[0] = first row = Apple, Price 1.50, Stock 200, Discount 0.10
editor.ItemContext = dt.Rows[0];Assigning a value to the Expression property prepopulates the editor with a predefined expression during form initialization.
The following expression multiplies the Price and Stock fields:
// Pre-fill the editor with a starting expression
editor.Expression = "[Price] * [Stock]";In the event handler, first verify that the expression is valid. Then, use TryEvaluate() to evaluate the expression against the configured data context.
editor.ExpressionChanged += Editor_ExpressionChangedEvaluate Expressions in the Event Handler
If evaluation succeeds, TryEvaluate() returns true and provides the evaluated value through the output variable. If evaluation fails, it returns false, and error information can be retrieved by using GetErrors().
private void Editor_ExpressionChanged(object sender, EventArgs e)
{
if (editor.IsValid) // check expression syntax is correct first
{
object result;
if (editor.TryEvaluate(out result)) // try to calculate
txtResult.Text = result.ToString(); // show result
else
txtResult.Text = editor.GetErrors()[0].ToString(); // show error
}
}