# Data Binding

## Content



CalcEngine supports data binding and uses it's default engine, i.e., VBA Engine, to bind to a collection. This collection can either be an array or a list. The example of data binding using an array is already demonstrated in the [Quick Start](/componentone/docs/services/online-calcengine/quick-start/quick-start-vba-engine) topic. In the following examples, we use a list to demonstrate data binding wherein we showcase data binding with a collection and its subset.

### Data Binding to a Collection

The following example demonstrates binding the collection to the engine using [DataSource](/componentone/api/services/online-calcengine/dotnet-standard-api/C1.CalcEngine/C1.CalcEngine.C1CalcEngine.DataSource.html) property of the [C1CalcEngine](/componentone/api/services/online-calcengine/dotnet-standard-api/C1.CalcEngine/C1.CalcEngine.C1CalcEngine.html) class and aggregate function to work with multiple values and evaluate the result.

```csharp
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}
var collection = new List<Customer>()
{
    new Customer { Id = 27, Name = "Den" },
    new Customer { Id = 15, Name = "Anna" }
};
// Working with collection and aggregate function:
var engine = new VBAEngine();
engine.DataSource = collection;
engine.Expression = "Sum([Id])";
Assert.AreEqual(42, engine.Evaluate());
```

### Data Binding to the Collection Subsets

The following example demonstrates binding the first record of the collection with the engine using the **DataSource** property to operate on a single data field and then using an expression to evaluate the result. This example uses the Customer class created in the [Data Binding to a Collection](/componentone/docs/services/online-calcengine/data-binding#data-binding-to-a-collection) section.

```csharp
// Binding with a single data entry:
var engine = new VBAEngine();
engine.DataSource = collection[0];
engine.Expression = "[Id] + 1";
Assert.AreEqual(28, engine.Evaluate());
```

In the following example, we demonstrates binding the collection to the engine using the **DataSource** property and setting the [CurrentIndex](/componentone/api/services/online-calcengine/dotnet-standard-api/C1.CalcEngine/C1.CalcEngine.C1CalcEngine.CurrentIndex.html) property to evaluate the expression for a specific entry.

```csharp
// Working with a collection to evaluate a single entry:
var engine = new VBAEngine();
engine.DataSource = collection;
engine.CurrentIndex = 1;
engine.Expression = "[Id] / Sum([Id])";
Assert.AreEqual(0.380952380952381, engine.Evaluate());
```