Data filtering in a grid can be performed in two ways:
In header-based filtering, an icon is displayed in the header cell of column which gives you options to specify the filter in a drop-down and also indicates if the filter is applied on a particular column. In FlexGrid, this behavior can be achieved by simply using AllowFiltering property of the grid. In this case, filter does not require any extra real estate on the screen. This approach also gives you flexibility to customize and create better filter editors. More details about same is given in the sections below.
Filter row is a row dedicated for displaying the filtering criteria and appears just below the column header. So, in this case, user can always see the filtered columns and their current filtering criteria. But, this added advantage comes at the cost of extra screen real estate that a filter row needs. In FlexGrid, you can easily achieve filter row using the custom implementation. For implementation, see the product sample named Filter Row.
In FlexGrid, filtering can be enabled by setting the C1FlexGrid.AllowFiltering property to true. This enables the default ColumnFilter on all columns of the grid. The ColumnFilter lets the users choose from ValueFilter and ConditionFilter at runtime.
You can also specify the filter types for each column by setting the AllowFiltering property of each Column object. AllowFiltering property of column lets you choose from any of the following values:
Value | Description |
---|---|
Default | Enables the ColumnFilter which is a combination of ValueFilter and ConditionFilter and gives user a runtime option to choose either of them. |
ByValue | Enables ValueFilter which contains a checkbox list of values present in that column. User can check off the values that should not be displayed in the filtered output. |
ByCondition | Enables ConditionFilter which lets you specify combination of two conditions such as "equals", "greater than", "contains" etc. You can combine the specified conditions using "And" and "Or" operator. |
Custom | Lets you instantiate a custom filter and explicitly assign it to Filter property of the column. |
None | Disables filtering for that column. |
Use the following code snippet to enable a condition filter on first column of the WinForms FlexGrid.
For more details regarding abovementioned filter types, see Types of Filters.
Although, AllowFiltering property of grid enables grid filtering and can be used in most common scenarios, there may be cases where you need to further fine tune the filter options. This can be achieved by modifying AllowFiltering and Filter properties of individual columns. For example, the code below enables filtering in the WinForms FlexGrid, but restricts the filtering to columns of type string.
You can also customize the filtering process further by creating filters and assigning them to columns, or by retrieving existing filters and modifying their properties. For example, the code below creates a ConditionFilter in the WinForms FlexGrid, configures it to select all items that are equal to "Germany", and then assigns the new filter to the "ShipCountry" column:
When filtering is applied to FlexGrid, it hides the rows that do not qualify the filtering criteria by setting their Visible property to false. However, in some cases, you may want to customize this behavior. For such scenarios, you can use the BeforeFilter and AfterFilter events fired by FlexGrid. The below example customizes the filtering behavior of WinForms FlexGrid by applying a different style to non-qualifying rows instead of hiding them.
The above code uses a custom style named "filteredout" which can be defined as shown in the code below.
User can remove column filtering at runtime by using Clear option given in the filter UI of each column. You can also remove filtering of the whole grid programmatically by passing the empty string in FilterDefinition property of FlexGrid.
Below code demonstrates how to clear all the filters from the WinForms FlexGrid.