Articles in this section
Category / Section

How to localize the filter values in GridCheckBoxColumn in WPF DataGrid (SfDataGrid)?

1 min read

You can localize the filter values in FilterPopup of GridCheckBoxColumn instead of loading Boolean values in WPF DataGrid (SfDataGrid). You can load different values using SfDataGrid.FilterItemsPopulated event and also you can apply filter based on the modified values using SfDataGrid.FilterChanging event like below code example.

 

XAML

<syncfusion:SfDataGrid x:Name="dataGrid"
                               AllowEditing="True"
                               AllowFiltering="True"
                               AllowGrouping="True"
                               AutoGenerateColumns="False"
                               ItemsSource="{Binding EmployeeDetails}"
                               ShowGroupDropArea="True">
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn MappingName="EmployeeName" />
                <syncfusion:GridTextColumn MappingName="EmployeeAge" />
                <syncfusion:GridTextColumn MappingName="EmployeeArea" />
                <syncfusion:GridTextColumn MappingName="EmployeeGender" />
                <syncfusion:GridTextColumn MappingName="ExperienceInMonth" />
                <syncfusion:GridCheckBoxColumn MappingName="Review" />
            </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

C#

public MainWindow()
{
    InitializeComponent();
    this.dataGrid.FilterItemsPopulated += grid_FilterItemsPopulated;
    this.dataGrid.FilterChanging += grid_FilterChanging;
}
 
void grid_FilterChanging(object sender, GridFilterEventArgs e)
{
    if (e.FilterPredicates == null || e.Column.MappingName != "Review" || e.FilterPredicates.Count == 0)
        return;
 
    //once the filter is applied, we need to change the filtervalue as original instead the changed value in filteritemspopulated event.
    if (e.FilterPredicates[0].FilterValue.Equals("Reviewed"))
        e.FilterPredicates[0].FilterValue = true;
    else if (e.FilterPredicates[0].FilterValue.Equals("NotReviewed"))
        e.FilterPredicates[0].FilterValue = false;
}
 
void grid_FilterItemsPopulated(object sender, GridFilterItemsPopulatedEventArgs e)
{
    if (e.Column.MappingName == "Review")
    {
        var itemsSource = e.ItemsSource as List<FilterElement>;
 
        //true changed into "Reviewed" and false changed into "NotReviewed" for localization.
        foreach (var element in itemsSource)
        {
            if (element.ActualValue.Equals(true))
                element.ActualValue = "Reviewed";
            else if (element.ActualValue.Equals(false))
                element.ActualValue = "NotReviewed";
        }
    }
}   

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied