Articles in this section
Category / Section

How to show the filter status message in WPF DataGrid (SfDataGrid)?

6 mins read

You can show the filter message while applying filter by creating custom FilterStatausBarControl in WPF DataGrid (SfDataGrid) and bind the SfDataGrid to DataGrid property of FilterStatusBarControl as like below.

<syncfusion:SfDataGrid x:Name="sfgrid"
                       Grid.Row="0"
                       FilterRowPosition="FixedTop"  
                       ItemsSource="{Binding Path=EmployeeDetails}"/>
<local:FilterStatusBar x:Name="FilterStatusBar"                                     
                       Grid.Row="1"                                                
                       DataGrid="{Binding ElementName=sfgrid,UpdateSourceTrigger=PropertyChanged}"/>

 

FilterStatusBarControl handles FiltercChanged event of WPF DataGrid (SfDataGrid) and updates the FilterText.

public class FilterStatusBar : Control
{
  /// <summary>
  /// Gets or sets the type of SfDataGrid binding with FilterStatusBar.
  /// </summary>     
    public SfDataGrid DataGrid
    {
        get { return (SfDataGrid)GetValue(DataGridProperty); }
        set { SetValue(DataGridProperty, value); }
    }
 
    /// <summary>
    /// Identifies the Syncfusion.UI.Xaml.Grid.FilterStatusBar.DataGrid dependency property.
    /// </summary>
    /// <remarks>
    /// The identifier for the Syncfusion.UI.Xaml.Grid.FilterStatusBar.DataGrid dependency property.
    /// </remarks>
 
    public static readonly DependencyProperty DataGridProperty =
        DependencyProperty.Register("DataGrid", typeof(SfDataGrid), typeof(FilterStatusBar), new PropertyMetadata(null));
 
    /// <summary>
    /// Gets or sets the string that is used to displayed on the FilterStatusBar in SfDataGrid.
    /// </summary>
    /// <value>
    /// The string that is used to display on the FilterStatusBar.
    /// </value>      
    public string FilterText
    {
        get { return (string)GetValue(FilterTextProperty); }
        private set { SetValue(FilterTextProperty, value); }
    }
 
    /// <summary>
    /// Identifies the Syncfusion.UI.Xaml.Grid.FilterStatusBar.FilterText dependency property.
    /// </summary>
    /// <remarks>
    /// The identifier for the Syncfusion.UI.Xaml.Grid.FilterStatusBar.FilterText dependency property.
    /// </remarks>
 
    public static readonly DependencyProperty FilterTextProperty =
        DependencyProperty.Register("FilterText", typeof(string), typeof(FilterStatusBar), new PropertyMetadata(null));
 
    #region Ctor
 
    /// <summary>
    /// Initializes the <see cref="FilterStatusBar"/> class.
    /// </summary>
    public FilterStatusBar()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(FilterStatusBar), new FrameworkPropertyMetadata(typeof(FilterStatusBar)));          
    }
 
    /// <summary>
    /// Initialize a new instance of the <see cref="FilterStatusBar"/> class.
    /// </summary>
    /// <param name="datagrid"></param>
    public FilterStatusBar(SfDataGrid datagrid)
    {
        DataGrid = datagrid;
    }
 
    /// <summary>
    /// Occurs when FilterStatusBar loaded.
    /// </summary>
    /// <param name="sender">FilterStatusBar</param>
    /// <param name="e">RoutedEventArgs</param>
    private void OnFilterStatusBarLoaded(object sender, RoutedEventArgs e)
    {
        DataGrid.FilterChanged += OnDataGridFilterChanged;
    }
        
    /// <summary>
    /// Occurs after the column is filtered in SfDataGrid. 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected virtual void OnDataGridFilterChanged(object sender, GridFilterEventArgs e)
    {
        string filters = "";            
        foreach (var item in this.DataGrid.Columns)
        {
            if (item != null && item.FilterPredicates.Count > 0)
            {
                foreach (var filterItem in item.FilterPredicates)
                {
                    if (filters == "")
                        filters += GetFilterInfo(filterItem, item);
                    else
                        filters += " " + this.GetPredicateType(filterItem.PredicateType) + " " + GetFilterInfo(filterItem, item);
                }
            }
        }
        UpdateFilterText(filters);
        this.UpdateFilterStatusBarVisibility(filters.Trim() != "");
    }
    
    /// <summary>
    /// Methed to Update the FilterText value.
    /// </summary>
    /// <param name="filter">The corresponding filtervalue.</param>
    /// <returns>Return the FilterStatusBar information from filtervalue.</returns>
    private string UpdateFilterText(string filter)
    {
        return this.FilterText = filter;
    }
}

 

FilterStatusBaseControl in application has support to clear and close options and provides support to display tooltip. You can find the ControlTemplate of FilterStatusBar control in sample.

SfDataGrid with FilterStatusBarControl

Filter status bar with WPF DataGrid

FilterStatusBar with Tooltip

Filter status bar with ToolTip

View sample in GitHub.

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