How to Create a Custom Data Filter for a WinForms Datagrid
FlexGrid provides built-in filtering support through the use of column filters. However, there may be a scenario where you would prefer to apply your own custom filtering instead. The ability to apply custom filters to your FlexGrid is a powerful tool that can provide deep insights into your data analysis.
The functionality is already built into the FlexGrid control’s library using the IC1ColumnFilter and IC1ColumnFilterEditor interfaces. In this blog, we’ll learn how to apply a custom filter such as "Last Fiscal Year" to a FlexGrid DateTime column in a .NET 6 application.
Let’s get started!
Ready to Start Creating? Download ComponentOne Today
Set up the Project
First, open Visual Studio and make a new .NET 6 WinForms application. We will be naming ours “FlexGridCustomFilters”; you’ll find a download link to our project at the end of this blog.
Next, we download the C1.Win.FlexGrid 6.0 .dll file to our project via the NuGet Package Manager. With that successfully installed, we can now drag and drop a FlexGrid onto our form. We add some dummy shipping data to a DataTable object that we then bind to our FlexGrid. The dummy shipping data goes back 800 days, day by day, allowing us to test out some custom date filtering. Please view the sample attached for more information on the dummy data generation.
Create a Custom Filter
Now that we have a FlexGrid with data filled in it, let’s begin creating a custom filter for the “Shipping Date” column. For reference, the “Shipping Date” column contains DateTime values that go back 800 days, or over two years, into the past. For the sake of this sample, let’s say we have been instructed to create a custom filter that will reload the grid to display only orders shipped “Last Week”, “This Fiscal Year”, or “Last Fiscal Year” when the appropriate checkbox is selected during filter selection at runtime, similar to the screenshot below:
To create a custom filter, you need to create a filter class that implements the IC1ColumnFilter interface, and an editor class that implements the IC1ColumnFilterEditor interface. The editor allows users to modify and configure the filter at runtime.
Create a Filter Class with IC1ColumnFilter
Let’s start with the filter class that implements the IC1ColumnFilter interface first. We’ll create a new file in our project called DateFilter.cs and implement the IC1ColumnFilter interface by writing the following during our class declaration:
Next, we create some fields to keep track of DateFilter values at runtime:
Then, we add in the object model’s getter and setter methods for the minimum and maximum values of our filter:
Lastly, we must include the required members implemented by the IC1ColumnFilter, which are the IsActive property, and the Reset(), Apply(), and GetEditor() methods, as shown below:
Create a Filter Editor with IC1ColumnFilterEditor
Next, we must add a new user control to our project, which will include our editor class that implements the IC1ColumnFilterEditor Interface. We will be naming this user control DateFilterEditor.cs and defining the public partial class as follows:
First, we should go into the designer view of the user control to add in some standard checkboxes, which we will utilize within our custom filter methods that will be contained within our DateFilterEditor class.
Since we want to see the results for “Last Week”, “This Fiscal Year”, and “Last Fiscal Year”, we should drag three checkboxes onto the user control and name them accordingly. We can set the logic behind what happens when these checkboxes are checked later in this guide.
Similar to the DateFilter class we made earlier, we have a region for fields and constructors:
Similar to our DateFilter class that implemented IC1ColumnFilter, for our DaterFilterEditor class, we need to implement IC1ColumnFilterEditor to allow our DateFilterEditor objects to inherit the type IC1ColumnFilterEditor through polymorphism via implementation of the interface.
When implementing the IC1ColumnFilterEditor interface, we have to create a KeepFormOpen property, as well as an Initialize() and ApplyChanges() method. The ApplyChanges() method is where we will apply the logic to our custom filter selections. The following will complete our setup of the custom filter:
Apply the Filter
To apply the filter to our FlexGrid, we must go back to our Form1.cs and set the filter for the “Ship Date” column to a new DateFilter object by calling the following line of code:
Now, we can run our application, and when we open the column filter editor for the “Ship Date” column, we’ll see the checkbox options from our DateFilterEditor user control class:
When we select the checkbox for one of our filters, it will apply the logic for the equivalent from inside our DateFilterEditor class’s ApplyChanges() method.
For example, if the “Last Week” checkbox is selected, the if(_chkLastWeek.Checked) block from within the ApplyChanges() method (inside our DateFilterEditor.cs class) will apply the proper filter logic on the displayed grid:
To summarize what we accomplished in this blog, all this custom filter logic works through object polymorphism. By implementing IC1ColumnFilter and IC1ColumnFilterEditor interfaces, our custom DateFilter and DateFilterEditor classes inherit the types IC1ColumnFilter and IC1ColumnFilterEditor, respectively.
The FlexGrid column filters allow themselves to be set to objects of type IC1ColumnFilter, which our DateFilter objects are as well, all thanks to the principle of polymorphism applied through interface implementation.
Ready to Start Creating? Download ComponentOne Today!