# Data Binding

## Content

Data binding enables the [C1ExpressionEditor](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.html) 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()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.Evaluate.html) or [TryEvaluate()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.TryEvaluate.html) method.

* The [Evaluate()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.Evaluate.html) method evaluates the current expression and returns the result directly.
* The [TryEvaluate()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.TryEvaluate.html) 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()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.TryEvaluate.html).

## Data Binding with DataSource and ItemContext

The [DataSource](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.DataSource.html) and [ItemContext](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.ItemContext.html) properties determine the data available for expression authoring and evaluation.

| Property | Role in This Example |
| -------- | -------------------- |
| [DataSource](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.DataSource.html) | Assigns the `DataTable` to ExpressionEditor. The table columns are exposed as fields that can be referenced in expressions, such as `[ProductName]`, `[Price]`, `[Stock]`, and `[Discount]`. |
| [ItemContext](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.ItemContext.html) | Specifies the `DataRow` against which the expression is evaluated. In this example, the first row of the `DataTable` is assigned explicitly so that field references resolve to values from that row. |

>type=note
> **<span data-loadable-vc-wrapper="true" data-ssr-placeholder-replace="Hm6N8:EfLS5:z8NN7:qz-Pe:OP-5u-0">Note:</span>**<span data-loadable-vc-wrapper="true" data-ssr-placeholder-replace="Hm6N8:EfLS5:z8NN7:qz-Pe:OP-5u-0"> </span>**<span data-loadable-vc-wrapper="true" data-ssr-placeholder-replace="Hm6N8:EfLS5:z8NN7:qz-Pe:OP-5u-0">ItemContext</span>**<span data-loadable-vc-wrapper="true" data-ssr-placeholder-replace="Hm6N8:EfLS5:z8NN7:qz-Pe:OP-5u-0"> 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.</span>

***

## Configuring Data-Driven Expressions

### Step 1: Create the DataTable

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]`.

```csharp
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;
}
```

***

### Step 2: Configure the DataSource Property

Assign the `DataTable` to the [DataSource](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.DataSource.html) property to populate IntelliSense with the available column names.
Configure the **data source** during form initialization or load.

```csharp
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;
```

***

### Step 3: Configure the ItemContext Property

Assign a `DataRow` to the [ItemContext](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.ItemContext.html) 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`.

```csharp
// dt.Rows[0] = first row = Apple, Price 1.50, Stock 200, Discount 0.10
editor.ItemContext = dt.Rows[0];
```

***

### Step 4: Initialize the Expression Property

Assigning a value to the [Expression](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.Expression.html) property prepopulates the editor with a predefined expression during form initialization.
The following expression multiplies the `Price` and `Stock` fields:

```csharp
// Pre-fill the editor with a starting expression
editor.Expression = "[Price] * [Stock]";
```

***

### Step 5: Handle the ExpressionChanged Event

In the event handler, first verify that the expression is valid. Then, use [TryEvaluate()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.TryEvaluate.html) to evaluate the expression against the configured data context.

```csharp
editor.ExpressionChanged += Editor_ExpressionChanged
```

**Evaluate Expressions in the Event Handler**
If evaluation succeeds, [TryEvaluate()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.TryEvaluate.html) 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()](/componentone/api/win/online-expressioneditor/dotnet-api/C1.Win.ExpressionEditor.10/C1.Win.ExpressionEditor.C1ExpressionEditor.GetErrors.html).

```auto
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
    }
}
```