[]
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.
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:
vbnet
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)
csharp
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:
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:
vbnet
Dim result As IEnumerable(Of Customer) = Customer.GetSampleCustomerList()
Dim cus = result.Where(Function(x) x.LastName = "Three")
_flexSheet.AddSheet("IEnumerable", result)
csharp
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: