Skip to main content Skip to footer

Add Dynamic Sort & Filter Options to Your .NET C# Datagrid Application

The C1DataEngine is a standalone package that provides high-performance analytics by extracting relevant information from large and complex datasets. Its columnar storage model uses memory-mapped files to offer remarkable performance, and it can handle millions of records in less than a second.

With C1DataEngine, you can get the Data from various sources and transform it according to your business needs. This transformation can be considered filtering, sorting, aggregating, and joining in, making the data accessible for the end-user to understand. 

This transformed data can be loaded into a visualization tool like DataGrid for rapid data insights that can be further used to forecast business outcomes.

In a real-world scenario, the developers want to make their application data-aware to make it more user-friendly. By data-aware, we mean that the end-user can change the data displayed via filtering or sorting as per their need.

In this blog, we will show you how to use the C1DataEngine API with DataGrid to allow end-users to easily sort and filter data by native DataGrid column headings and filter columns as users type using text boxes by following the steps given below:

Create WindowsForm Application

Create a .NET 6.0 Windows Forms App in Visual Studio 2022 and drop the following controls onto the form:

  • DataGridView – To show the data
  • TextBox - For the end-user to be used as a filter text input for a particular field. The end goal is to filter the grid as user types in TextBox
  • StatusStrip - Add the StatusLabel to the StatusStrip control using the dropdown icon to show the count of rows in the grid

    The form should be like this:

Data Image Collection Sample

Add Required NuGet Package 

To use the DataEngine API to load the large data and show it in the grid, add the following packages to the project:

Assign the Data to the Grid 

First, initialize the Workspace to get the data and the IDataCollection object to get the queried data assigned to DataGrid.

private Workspace _workspace;
private IDataCollection<object> _dataCollection;

Then create a LoadAsync function to load the data from the DataService class (taken from the DataSource.cs file in the DataEngineCollectionSample).

The LoadAsync method will look like the following:

private async Task LoadAsync()
{
toolStripStatusLabel1.Text = "Generating data...";
await Task.Yield();
_workspace = new Workspace();
_workspace.Init("workspace");
if (!DataService.TablesExist(_workspace))
{
await DataService.GenerateData(_workspace);
}
_dataCollection = await DataService.LoadDataCollection(_workspace);
dataGridView1.DataSource = new C1DataCollectionBindingList(_dataCollection);
_dataCollection.CollectionChanged += _dataCollection_CollectionChanged;
this.FormClosed += (s, e) => ((IDisposable)_dataCollection).Dispose();
UpdateStatus();
}

Update ToolStripStatusLabel as Data Changes in the Grid

As you can see in the code in the last section, a method called UpdateStatus is used to update the grid’s current row count and show it in the ToolStripStatusLabel. We will also use this method in the CollectionChanged event handler of DataCollection, which occurs when the collection changes.


The code looks like the below:

private void UpdateStatus()
{
toolStripStatusLabel1.Text = string.Format("Count: {0}", _dataCollection.Count);
}                      
 
private void _dataCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
UpdateStatus();
}

 
Add Option for Runtime Filtering 

Add the FilterAsync method of the DataCollection so that you can filter asynchronously from the list of data in the grid. The filtering functionality has been added for the FirstName column in this demo.

This method passes the FilterExpression property of C1DataEngineCollection class, which gets the filter expression applied to the data.

You need to add this method to the TextChanged event of the TextBox so that it can get fired as user types:

private void searchBox_TextChanged(object sender, System.EventArgs e)
{
_dataCollection.FilterAsync(FilterExpression.FromString(searchBox.Text, new string[] { "FirstName" }));
}

The DataCollection has inbuilt Sorting functionality, so you need not add extra code for the column sorting feature. The data will be sorted automatically as the user clicks on the column header.

The final output will work like the GIF given below:

Final Output

You can also download the sample from the Github link:
DataEngineCollectionSample    

Please feel free to try it out and leave your feedback or questions in the comments section.

Happy Coding!

comments powered by Disqus