Articles in this section
Category / Section

How to skip the frozen row data from filtering in the UWP SfDataGrid?

4 mins read

The SfDataGrid provides support to freeze the rows and columns like Excel’s FreezePanes. By default, the filtering feature allows you to filter the data in the rows based on particular criteria.  Initially, the Filter Popup is loaded with the FilterElements and the value of the FilterElements is the display value of the corresponding column. Once you filter, the FilterElements are reset based on the specified conditions. It does not have any special option to skip freezing the row data on filtering.

The following screenshot illustrates how the Filter Popup is loaded with its FilterElements when the FrozenRowsCount is defined in the SfDataGrid.

 

SF DataGrid

Figure 1: Default view of frozen rows in Filtering

In the SfDataGrid, it is possible to skip the frozen row data and maintain these frozen rows in view of the SfDataGrid on filtering. This can be achieved by using the FilterItemsPopulated and FilterChangingEvent events of the SfDataGrid.

These events are wired as shown in the following code example.

C#

public MainWindow()
{
    InitializeComponent();
    //FilterItemsPopulated event is wired.
    this.sfdatagrid.FilterItemsPopulated += sfdatagrid_FilterItemsPopulated;
    //FilterChanging event is wired.
    this.sfdatagrid.FilterChanging += sfdatagrid_FilterChanging;
}

 

The FilterItemsPopulated event is raised when the filter item list is populated and the ItemsSource of the GridFilterControl is set.  With the help of this event, you can manage filter values and avoid frozen row data being populated into the filter item list as shown in the following code example.

C#

void sfdatagrid_FilterItemsPopulated(object sender, GridFilterItemsPopulatedEventArgs e)
{
    //Checks whether the rows freeze in the SfDataGrid.
    //When true, the default value is loaded in FilterElement.
    if (this.sfdatagrid.FrozenRowsCount < 1)
        return;
    //Gets the columnname to be applied on Filtering.
    var columnName = e.Column.MappingName;
    //Gets property access provider to get frozen rows display value.
    var provider = this.sfdatagrid.View.GetPropertyAccessProvider();           
    //Gets the corresponding FilterElement of the column.
    var itemsSource = e.ItemsSource as List<FilterElement>;
    //Checks whether the Grouping is applied or not.
    //When Grouping is not applied,you can get record directly from View. 
    if (this.sfdatagrid.GroupColumnDescriptions != null && this.sfdatagrid.GroupColumnDescriptions.Count < 1)
    {
        //Gets the records from view of the SfDataGrid.
        var records = this.sfdatagrid.View.Records;
        //Gets the value for frozen rows count of corresponding column and removes it from FilterElement collection.
        for (int i = 0; i < this.sfdatagrid.FrozenRowsCount && i < this.sfdatagrid.View.Records.Count; i++)
        {
            var value = provider.GetValue(records[i].Data, columnName);
            if (value != null)
            {
                //Removes from FilterElement collection.
                var removeElement = itemsSource.FirstOrDefault(item => (item.ActualValue != null && item.ActualValue.Equals(value)) || item.ActualValue == null);
                itemsSource.Remove(removeElement);
            }
        }
    }
    //When Grouping is applied, gets the records DisplayElements of the TopLevelGroup in view.
    else
    {
        //Gets the records from DisplayElements.
        var records = this.sfdatagrid.View.TopLevelGroup.DisplayElements;
        for (int i = 0; i < this.sfdatagrid.FrozenRowsCount && i < this.sfdatagrid.View.TopLevelGroup.DisplayElements.Count; i++)
        {
            if (records[i] is RecordEntry)
            {
                //Gets the value for frozen rows count of corresponding column and removes it from the FilterElement collection.
                var value = provider.GetValue((records[i] as RecordEntry).Data, columnName);
                if (value != null)
                {
                    //Removes from FilterElement collection.
                    var removeElement = itemsSource.FirstOrDefault(item => (item.ActualValue != null && item.ActualValue.Equals(value)) || item.ActualValue == null);
                    itemsSource.Remove(removeElement);
                }
            }
        }
    }
}    

 

From the above code, you can get the filter values collection from the e.ItemSource argument of the GridFilterItemsPopulatedEventArgs class. To get the frozen rows count, use the FrozenRowsCount property of the SfDataGrid.  Then remove those frozen rows from the e.ItemSource collection. This is represented in the following screenshot.

Frozen data

Figure 2: Frozen row data removed from Filter values

Retain the frozen row data in view by using the FilterChanging event of the SfDataGrid and that is used to manage the FilterPredicates collection. In the code, the frozen row data is added to the filter predicates by using the e.FilterPredicates argument.

C#

void sfdatagrid_FilterChanging(object sender, GridFilterEventArgs e)
{
    //Checks whether Filtering is applied and the ForzenRowCount is more than zero 
    //When true,the default filtering operation is performed.
    if (e.FilterPredicates == null || this.sfdatagrid.FrozenRowsCount < 1)
        return;
    //Gets property access provider to get frozen rows display value.
    var provider = this.sfdatagrid.View.GetPropertyAccessProvider();
    //Gets the columnname to be applied on Filtering.
    var columnName = e.Column.MappingName;
    //Checks whether the Grouping is applied or not.
    //When Grouping is not applied, you can get the record directly from View.
    if (this.sfdatagrid.GroupColumnDescriptions != null && this.sfdatagrid.GroupColumnDescriptions.Count < 1)
    {
        //Gets the records from the View.
        var records = this.sfdatagrid.View.Records;
        //Adds the FrozenRows filter predicate to view.
        for (int i = 0; i < this.sfdatagrid.FrozenRowsCount; i++)
        {
            var value = provider.GetValue(records[i].Data, columnName);
            AddPredicates(e.FilterPredicates, FilterType.Equals, PredicateType.Or, value);
        }
    }
    //On Grouping, gets the records DisplayElements of the TopLevelGroup in view.
    else
    {
        //Gets the records from DisplayElements.
        var records = this.sfdatagrid.View.TopLevelGroup.DisplayElements;
        //Gets the value for frozen rows count of corresponding column and adds it to the FilterPredicate collection.
        for (int i = 0; i < this.sfdatagrid.FrozenRowsCount; i++)
        {
            if (records[i] is RecordEntry)
            {
                var value = provider.GetValue((records[i] as RecordEntry).Data, columnName);
                AddPredicates(e.FilterPredicates, FilterType.Equals, PredicateType.Or, value);
            }
        }
    }
}
//FilterPredicate is added here.
private void AddPredicates(List<FilterPredicate> FilterPredicates, FilterType FilterType, PredicateType predicateType, object value)
{
    FilterPredicates.Add(new FilterPredicate()
    {
        FilterValue = value,
        FilterType = FilterType,
        FilterBehavior = FilterBehavior.StronglyTyped,
        IsCaseSensitive = true,
        PredicateType = predicateType
    });
}

 

The following screenshot illustrates that after filtering, the frozen row data is retained in view of the SfDataGrid:

Graphical user interface, application

Description automatically generated

Figure 3: Frozen row data retained in view after applied Filtering



Conclusion

I hope you enjoyed learning about how to skip the frozen row data from filtering in the UWP SfDataGrid.

You can refer to our UWP SfDataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our UWP SFDataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

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