Articles in this section
Category / Section

How to load the symbols in FilterRow in WPF DataGrid (SfDataGrid)?

2 mins read

You can load symbols for FilterRowConditions in WPF DataGrid (SfDataGrid) FilterRow by overriding the GridFilterRowCell. You can also write the custom FilterRowRenderer to filter the records based on that loaded symbols.

Refer to the following code snippets to load symbols for FilterRowConditions.

XAML:

<syncfusion:SfDataGrid x:Name="grid"
                               AllowEditing="True"
                               FilterRowPosition="Top"
                              AutoGenerateColumns="False"                              
                              ColumnSizer="Star"
                              ItemsSource="{Binding Source}
 
   <syncfusion:SfDataGrid.Columns>
    
    <syncfusion:GridTextColumn HeaderText="Income" TextAlignment="Right" 
                            FilterRowEditorType="CustomNumericFilterRowRenderer"
                            MappingName="Salary" 
                            DisplayBinding="{Binding Salary,StringFormat={}{0:c}}"/>
 
  </syncfusion:SfDataGrid.Columns>

 

GridFilterRowCellExt.cs:

public class GridFilterRowCellExt : GridFilterRowCell
{
    private const string EqualsSymbol = "=";
    private const string NotEquals = "<>";
    private const string After = ">";
    private const string AfterOrEqual = ">=";
    private const string Before = "<";
    private const string BeforeOrEqual = "<=";
 
 
    public override void OpenFilterOptionPopup()
    {
 
        base.OpenFilterOptionPopup();
 
        if (DataColumn.GridColumn is GridTextColumn && DataColumn.GridColumn.MappingName == "Salary")
        {
            FilterOptionsList.ItemsSource = IncomeOptions();
        }
    }
 
 
    private ObservableCollection<string> IncomeOptions()
    {
        var list = new ObservableCollection<string>
        {
            EqualsSymbol,
            NotEquals,
            After,
            AfterOrEqual,
            Before,
            BeforeOrEqual
        };
        return list;
    }
}
 

 

CustomNumericFilterRowRenderer.cs:

public class CustomNumericFilterRowRenderer : GridFilterRowCellRenderer<ContentControl, TextBox>
{
 
    private TextBox textBox;
    public override void OnInitializeDisplayElement(DataColumnBase dataColumn, ContentControl uiElement, object dataContext)
    {
        GridColumn gridColumn = dataColumn.GridColumn;
        Binding binding1 = new Binding
        {
            Path = new PropertyPath("FilterRowText"),
            Mode = BindingMode.TwoWay,
            Source = gridColumn,
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        };
 
    }
 
    protected override void OnWireEditUIElement(TextBox uiElement)
    {
        uiElement.TextChanged += UiElement_TextChanged; ;
    }
 
    private void UiElement_TextChanged(object sender, TextChangedEventArgs e)
    {
        var textBox = sender as TextBox;
        ProcessSingleFilter(textBox.Text);
 
    }
 
    protected override void OnUnwireEditUIElement(TextBox uiElement)
    {
        uiElement.TextChanged -= UiElement_TextChanged;
    }
 
    protected override void OnEditElementLoaded(object sender, RoutedEventArgs e)
    {
        textBox = sender as TextBox;
        var uiElement = textBox;
        uiElement.Focus();
        uiElement.TextChanged += UiElement_TextChanged;
    }
 
      
 
    public override void OnFilterRowConditionChanged(string filterRowCondition)
    {
        filterRowCondition = GetFilterRowCondition(filterRowCondition);
 
        base.OnFilterRowConditionChanged(filterRowCondition);
        ProcessSingleFilter(textBox.Text);
    }
 
    private string GetFilterRowCondition(string filterType)
    {
        if (filterType == "=")
            return GridResourceWrapper.Equalss;
        else if (filterType == "<>")
            return GridResourceWrapper.NotEquals;
        else if (filterType == ">")
            return GridResourceWrapper.GreaterThan;
        else if (filterType == ">=")
            return GridResourceWrapper.GreaterThanorEqual;
        else if (filterType == "<")
            return GridResourceWrapper.LessThan;
        else if (filterType == "<=")
            return GridResourceWrapper.LessThanorEqual;
        return GridResourceWrapper.Equalss;
    }
 
    public override string GetFilterText(List<FilterPredicate> filterPredicates)
    {
        return textBox.Text;
    }
 
 
    private new List<FilterPredicate> GetFilterPredicates(object filterValue)
    {
        if (filterValue == null)
            filterValue = textBox.Text ?? "";
        
 
        FilterType filterType;
        object value = filterValue;
        switch (FilterRowCell.DataColumn.GridColumn.FilterRowCondition)
        {
            case FilterRowCondition.Equals:
                filterType = FilterType.Equals;
                break;
            case FilterRowCondition.NotEquals:
                filterType = FilterType.NotEquals;
                break;
            case FilterRowCondition.Before:
                filterType = FilterType.LessThan;
                break;
            case FilterRowCondition.BeforeOrEqual:
                filterType = FilterType.LessThanOrEqual;
                break;
            case FilterRowCondition.After:
                filterType = FilterType.GreaterThan;
                break;
            case FilterRowCondition.AfterOrEqual:
                filterType = FilterType.GreaterThanOrEqual;
                break;
            case FilterRowCondition.Null:
                filterType = FilterType.Equals;
                value = null;
                break;
            case FilterRowCondition.NotNull:
                filterType = FilterType.NotEquals;
                value = null;
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
 
        return new List<FilterPredicate>{new FilterPredicate
        {
            FilterBehavior = FilterBehavior.StronglyTyped,
            FilterMode = ColumnFilter.Value,
            FilterType = filterType,
            FilterValue = value,
            IsCaseSensitive = false,
            PredicateType = PredicateType.OrElse
        }};
    }
}
 

Mainwindow.xaml.cs:

this.dataGrid.FilterRowCellRenderers.Add("CustomNumericFilterRowRenderer", new CustomNumericFilterRowRenderer());
this.dataGrid.RowGenerator = new CustomRowGenerator(dataGrid);

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