How to clear the filtering for all columns using HeaderContextMenu?
Filters applied to SfDataGrid can be cleared by calling SfDataGrid.ClearFilters. As the requirement is to clear filter using HeaderContextMenu, define ContextMenu for Header using SfDataGrid.HeaderContextMenu property. In the following XAML code, the ContextMenu is defined with one MenuItem for HeaderContextMenu.
XAML
<syncfusion:SfDataGrid x:Name="datagrid" AllowFiltering="True" AllowFrozenGroupHeaders="True" AutoExpandGroups="True" AutoGenerateColumns="False" ColumnSizer="Auto" ItemsSource="{Binding EmployeeDetails}" LiveDataUpdateMode="AllowDataShaping" ShowGroupDropArea="True"> <syncfusion:SfDataGrid.HeaderContextMenu> <ContextMenu Style="{x:Null}"> <MenuItem Command="{Binding Source={x:Static Member=local:ContextMenuCommands.ClearFiltering}}" CommandParameter="{Binding}" Header="Clear Filtering"> <MenuItem.Icon> <Path Height="17" Margin="3" HorizontalAlignment="Center" VerticalAlignment="Center" Data="F1M578.04,215.9512L568.419,215.8282L568.419,215.8272L568.4,215.8282L568.381,215.8272L568.381,215.8282L558.76,215.9512C557.435,215.9682,557.07,216.8432,557.868,217.8992C557.868,217.8992,565.497,225.9902,565.426,225.9912C565.397,225.9922,565.415,226.1842,565.445,226.4952L565.445,231.6702C565.445,232.6302,566.059,233.4082,566.816,233.4082L568.381,233.4082L568.419,233.4082L569.983,233.4082C570.741,233.4082,571.355,232.6302,571.355,231.6702L571.355,226.4952C571.385,226.1842,571.402,225.9922,571.374,225.9912C571.302,225.9902,578.932,217.8992,578.932,217.8992C579.73,216.8432,579.365,215.9682,578.04,215.9512" Fill="#FF5A5A5B" Stretch="Fill"> </Path> </MenuItem.Icon> </MenuItem> </ContextMenu> </syncfusion:SfDataGrid.HeaderContextMenu> </syncfusion:SfDataGrid>
The Command property in MenuItem is bound with the ContextMenuCommand.ClearFiltering command that is defined in the code example. The following code example is for ContextMenuCommands static class with ClearFiltering command property in it. MenuItem in HeaderContextMenu gets the GridColumnContextMenuInfo as command parameter, when the action is triggered. GridColumnContextMenuInfo has the following properties, from which you can clear the filters for particular column or for the all GridColumns.
- Column – It contains GridColumn
- DataGrid – It contains SfDataGrid
In the following ClearFiltering action, the filters are cleared for all columns by using SfDataGrid.ClearFilters() method.
C#
public static class ContextMenuCommands { #region ClearFiltering static BaseCommand clearFiltering; public static BaseCommand ClearFiltering { get { if (clearFiltering == null) clearFiltering = new BaseCommand(OnClearFilteringClicked); return clearFiltering; } } private static void OnClearFilteringClicked(object obj) { if (obj is GridColumnContextMenuInfo) { var dataGrid = (obj as GridColumnContextMenuInfo).DataGrid; dataGrid.ClearFilters(); } } #endregion }
Now, you can see ContexMenu when you right click on the column header of SfDataGrid, as in the following screenshot.
You can refer the sample from the following location,