FlexSheet for WPF | ComponentOne
Working with C1FlexSheet / Sheet Operations / Data Binding
In This Topic
    Data Binding
    In This Topic

    You can easily perform data binding in FlexSheet. Here, we will discuss about two types of data binding: Data binding using data source and data binding using IEnumerable Interface.

    Data Binding using Data Source

    To bind C1FlexSheet to a data source, we have used C1NWind.mdb database. You can find the C1NWind.mdb database in the Documents\ComponentOne Samples\Common directory.

    The following code binds the C1FlexSheet control to the C1NWind.mdb database:

    Dim con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data 
            Source=C:\Users\Windows 8.1\Documents\ComponentOne Samples\Common\C1NWind.mdb")
    Dim adpt As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Products", con)
    Dim dt As New System.Data.DataTable()
    adpt.Fill(dt)
    _flexSheet.AddSheet("Sheet1", dt.DefaultView)
    
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
       Source=C:\Users\Windows 8.1\Documents\ComponentOne Samples\Common\C1NWind.mdb");
    OleDbDataAdapter adpt = new OleDbDataAdapter("SELECT * FROM Products", con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    _flexSheet.AddSheet("Sheet1", dt.DefaultView);
    

    The above code retrieves the data from the Products table of C1NWind.mdb database. The data reflected in the FlexSheet after binding with data source is shown in the image below:
    DataSource Binding

    Data Binding using IEnumerable<T> Interface

    IEnumerable<T> interface inherits from the IEnumerable interface. To bind the data using IEnumerable <T> interface, a user-defined class needs to be added in the code. This class must implement the IEnumerable interface. In our case, we have created a class named Customer to implement the IEnumerable interface.

    The following code implements the IEnumerable interface:

    Dim result As IEnumerable(Of Customer) = Customer.GetSampleCustomerList()
    Dim cus = result.Where(Function(x) x.LastName = "Three")
    _flexSheet.AddSheet("IEnumerable", result)
    
    IEnumerable<Customer> result = Customer.GetSampleCustomerList();
    var cus = result.Where(x => x.LastName == "Three");
    _flexSheet.AddSheet("IEnumerable", result);
    

    The above code retrieves the data from a list of customers and their details defined in Customers class. The data reflected in the FlexSheet after binding with IEnumerable<T> interface is shown in the image below:
    Binding with IEnumerable interface