CalcEngine for .NET
In This Topic
    Data Binding
    In This Topic

    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 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 property of the C1CalcEngine class and aggregate function to work with multiple values and evaluate the result.

    C#
    Copy Code
    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 section.

    C#
    Copy Code
    // 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 property to evaluate the expression for a specific entry.

    C#
    Copy Code
    // 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());