Experience Seamless Excel-Like Filtering in WinUI DataGrid 

Jollen Moyani - Jan 10 '23 - - Dev Community

Filtering is one of the most necessary features of any data grid. With it, users can find the data they require from a huge volume of data sets by applying criteria.

The WinUI DataGrid control is used to display and manipulate tabular data efficiently. Its rich feature set includes data binding, editing, sorting, filtering, grouping, and data virtualization. It has been optimized to work with millions of records and handle high-frequency, real-time updates.

It also provides Excel-like filtering support to filter the rows more intuitively.

In this blog, we will see the various Excel-like filtering options available in the WinUI DataGrid.

Getting started with Excel-like filtering

We can enable Excel-like filtering support for all columns by setting the SfDataGrid.AllowFiltering property to true. The filter icon will be shown in the column headers.

Refer to the following image.

Enabling Excel-Like Filtering in WinUI DataGrid

Enabling Excel-Like Filtering in WinUI DataGrid

By clicking on the filter icon, you can see the Excel-like pop-up menu, as shown in the following image.

WinUI DataGrid with Excel-Like Filtering Pop-Up

WinUI DataGrid with Excel-Like Filtering Pop-Up

Features of Excel-like filtering in WinUI DataGrid

The Excel-like filtering support in our WinUI DataGrid has almost all the options available in the Microsoft Excel filtering menu:

  • Sorting: Easily sort the rows in ascending or descending order.
  • Clear filter: Clear the current filter applied to the column.
  • Advanced filtering.
  • Checkbox filtering.

Let’s look at the latter two options in more detail.

Advanced filtering

With advanced filtering, users can filter for the required data by applying multiple filter conditions with filter criteria. Each type of filter has its own set of filter conditions to apply filtering with ease.

The advanced filtering options are available based on the data type of the columns.

Supported data types:

  • Text
  • Number
  • Date

The following table shows the list of filter conditions supported in advanced filter options.

Text

Number

Date

· Equals

· Does Not Equal

· Begins With

· Does Not Begin With

· Ends With

· Does Not End With

· Contains

· Does Not Contain

· Empty

· Not Empty

· Null

· Not Null

· Equals

· Does Not Equal

· Null

· Not Null

· Less Than

· Less Than or Equal

· Greater Than

· Greater Than or Equal

· Equals

· Does Not Equal

· Before

· Before Or Equal

· After

· After or Equal

· Null

· Not Null

You can also choose whether the AND or OR logical operator should be applied between the two filter conditions you apply in the advanced filter.

Refer to the following GIF image.

Advanced Filtering Support in WinUI DataGrid

Advanced Filtering Support in WinUI DataGrid

Checkbox filtering

Checkboxes are the most common filtering option used to filter rows. By using a checked ListBox in the filter pop-up, you can easily select the data you want to view. A blank item is added automatically if the column has null or empty data.

Checkbox Filtering Support in WinUI DataGrid

Checkbox Filtering Support in WinUI DataGrid

Events

The following events will be called when we apply filtering through the Excel-like filter pop-up:

  • FilterChanging: This is invoked while applying filtering to a specific column.
  • FilterChanged: This is invoked after the filtering is applied to a specific column.
  • FilterItemsPopulating: This is invoked while populating a list of items in the checked list box.
  • FilterItemsPopulated: This is invoked after the list of items in the checked list boxes is populated.

Programmatic filtering

Along with the Excel-like filtering UI, you can also filter data programmatically through:

  • View filtering
  • Column filtering

View filtering

This feature enables users to apply logic to an entire row. This approach requires a bool return type method with an underlying data object as the parameter. This user-defined method will be called for every row in the DataGrid, and the user can return true or false based on the logic. If the return value is true , then that row will be filtered. Other rows will not be displayed in the view.

View filtering can be achieved by setting the SfDataGrid.View.Filter delegate. You can refresh the view by calling the SfDataGrid.View.RefreshFiltermethod.

Refer to the following code example.

public bool FilterRecords(object data)
{
    string filterText = "Germany";
    var item = data as OrderInfo;

    if (item != null)
    {
        if (item.Country.Equals(filterText))
            return true;
    }
    return false;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (sfDataGrid != null && sfDataGrid.View != null)
    {
        sfDataGrid.View.Filter = FilterRecords;
        sfDataGrid.View.RefreshFilter();
    }
}

Enter fullscreen mode Exit fullscreen mode

Refer to the following image.

View Filtering in WinUI DataGrid

View Filtering in WinUI DataGrid

Column filtering

This approach enables users to apply the filter conditions based on specific columns. So, rows will be filtered based on the specific column criteria.

Column filtering can be done using the GridColumn.FilterPredicates property by adding the FilterPredicate to it. You can specify your required criteria in the filter predicate.

In the following example, we will use the OrderID column to filter the data, which has a 1005 value.

sfDataGrid.Columns["OrderID"].FilterPredicates.Add(new FilterPredicate() { FilterType = FilterType.Equals, FilterValue = "1005" });
Enter fullscreen mode Exit fullscreen mode

Refer to the following image.

Programmatic Column Filtering in WinUI DataGrid

Programmatic Column Filtering in WinUI DataGrid

References

For more details, refer to the Excel-like filtering in the WinUI DataGrid GitHub demo and documentation.

Conclusion

Thanks for reading! I hope you enjoyed this blog about Excel-like filtering in Syncfusion’s WinUI DataGrid control. Try out this user-friendly feature and experience seamless filtering!

For current Syncfusion customers, the newest version of Essential Studio is available from the license and downloads page. If you are not yet a customer, you can try our 30-day free trial to check out these new features.

If you have questions, contact us through our support forums, feedback portal, or support portal. We are always happy to assist you!

Related blogs

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .