Apply Data Virtualization in FlexGrid for WinForms with OData
The ComponentOne 2020 v1 release includes new data-related service components, DataConnectors and DataCollection.
DataConnectors is a cross-platform, data connectivity library for connecting to popular data sources such as OData and Dynamics 365. DataCollection is a cross-platform data-binding library that provides support for filtering, sorting, and grouping data.
Along with these data-manipulation features, DataCollection supports data virtualization for incrementally loading large data sets.
In this post, we demonstrate how to integrate these two service components to use data virtualization with an OData service and bind the same to a data-aware control such as FlexGrid for WinForms.
Create your application with the following steps:
- Install the required NuGet packages
- Connect to an OData Web API service using C1AdoNetVirtualDataCollection
- Bind FlexGrid with C1AdoNetVirtualDataCollection
Step 1: Install Required NuGet Packages
Create a WinForms project and install the following NuGet packages:
- C1.AdoNet.OData
- C1.DataCollection.AdoNet
- C1.DataCollection.BindingList
To add a NuGet package, right-click the ‘References’ node in the ‘Solution Explorer’ window and select ‘Manage NuGet Packages’:
Step 2: Connect to an OData Web API Service Using C1AdoNetVirtualDataCollection
The DataCollection service library provides the base class C1VirtualDataCollection that implements pagination.
This class has an abstract method GetPageAsync which must be implemented to return the items that will populate the collection.
The library also provides the following classes:
These classes implement all the required methods so you can use them directly in your applications. The properties exposed by C1VirtualDataCollection can be used to control the virtualization operation.
You can configure the C1VirtualDataCollection objects with the following options:
Virtualization Mode:
This mode defines how the data will be loaded in the virtual data collection.
- Automatic (Default option): Loads data as the items are accessed
- Manual: Loads data by calling the LoadAsync method
Page Size:
Determines the number of rows requested in each fetch request.
For this example, use Northwind OData Web APIs, and fetch the records from the Orders table.
string connectionString = @"Url=https://services.odata.org/Experimental/Northwind/Northwind.svc/";
var odataConnection = new C1ODataConnection(connectionString);
var collectionView = new C1AdoNetVirtualDataCollection<Order>(odataConnection, "Orders");
collectionView.PageSize = 100;
We use the generic class C1AdoNetVirtualDataCollection<T>
to get strongly-typed records from the data source.
You can use the non-generic class C1AdoNetVirtualDataCollection, which creates an appropriate type for the records at runtime (you can access the type of information from the ‘ItemType’ property).
Step 3: Bind C1FlexGrid with C1AdoNetVirtualDataCollection
We wrap our virtual data collection object in C1DataCollectionBindingList to bind it to an instance of FlexGrid.
_flexGrid.BeginInvoke(new MethodInvoker(() =>
{
_flexGrid.DataSource = new C1DataCollectionBindingList(collectionView);
_flexGrid.AllowFiltering = true;
}));
Since the data collection is populated on a different thread, we use the BeginInvoke method of FlexGrid to avoid cross-thread exceptions for changing the DataSource.
Run our sample. The grid initially has 100 rows (configured using the PageSize property). As we scroll down, more data is fetched automatically
The complete sample is available here.
Read more about DataCollection and DataConnector here.