Prevent filtering out certain rows in Blazor Syncfusion DataGrid

I'm looking to remove certain filter choices in filter menu in Blazor Syncfusion DataGrid so that users cannot unselect them.

I basically want: "The top N records based on sort order should not be able to be filtered out".

To demonstrate what I'm looking for, I created this repro. You can run it yourself.

https://blazorplayground.syncfusion.com/VtVoNmCjPZjeFpML

I want Model2Ord1 whose sort order is 1 to NOT be able to be filtered out.

I want something like this where Model2Ord1 is removed/ not visible on the filter choices, but other choices are visible:

repro-grid-remove-choice.png
Basically, I'd like to do something like below, but it doesn't work:
    public void FilterDialogOpeningHandler(FilterDialogOpeningEventArgs args)
    {
        args.CheckboxListData = gridData.Where(d => d.SortOrder > 1).Select(d => d.Model).ToList();
    }


I need help accomplishing these 2 things:

  1. Remove filter choices based on certain logic (sort order in this case), so user can never filter out certain choices, and those records are always visible.
  2. Make sure the filter choices appear based on the sort order of `gridData`. Currently as you can see, the filter choices appear as Model1Ord2, Model2Ord1, Model3Ord4, Model4Ord3. I want them to show up in correct order like: Model2Ord1 (Obviously I want this hidden/removed), Model1Ord2, Model4Ord3, Model3Ord4.


Thanks!


11 Replies

PS Prathap Senthil Syncfusion Team July 11, 2025 11:57 AM UTC

Hi Ashish Khanal,


Based on the information provided, it seems that the FilterDialogOpening event was not defined in the grid events in your shared sample. We have updated the code to address this issue and have implemented the appropriate logic for the CheckboxListData. Please refer to the code snippet and sample below for your reference.


public void FilterDialogOpeningHandler(FilterDialogOpeningEventArgs args)

{

 

     if (args.ColumnName == "Model")

     {

 

         List<GridRowViewModel> newList = gridData

             .Where(d => d.SortOrder > 1) // Filter out items with SortOrder <= 1

             .OrderBy(d => d.SortOrder)   // Sort the list by SortOrder

             .ToList();

   

         args.CheckboxListData = newList.DistinctBy(x => x.Model).ToList();

 

  

     }

 

 

}



Sample: https://blazorplayground.syncfusion.com/embed/VXrojQiWAhThPsyu?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5

Regards,
Prathap Senthil



AK Ashish Khanal July 11, 2025 09:24 PM UTC

Hi Prathap,

Thank you for the response.

Your solution works to limit the filter choices, but it still doesn't prevent filtering out the rows of that model.


Let me explain:

- I want to hide the choice whose rows I don't want filtered out. Model2Ord1 is hidden from filter choices. This is good. ✅

prevent-1.png

- When I select some other model, Model2Ord1 should always be visible. I need to prevent its rows from being filtered out. I want it to be always shown (Basically what I want is: "N models should always be shown in the grid, so they need to be hidden in the filter menu choices").

Currently Model2Ord1 rows get filtered out if for example I select Model1Ord2 which is not the behavior I want. 🛑

I want [Model2Ord1 (always) Model1Ord2] ​​rows when I select Model1Ord2.

prevent-2.png


I'm trying below code but it doesn't seem to work correctly. It works correctly when I select 2 or more choices, but it doesn't work correctly when I select just 1 choice.

Check out the runnable sample here: https://blazorplayground.syncfusion.com/hjrytwCsmQDVbLKh


Works with 2 or more selection (as you can see Model2Ord1 is included here and is not filtered out):


Doesn't work with 1 selection (as you can see Model2Ord1 is not included in the grid and is filtered out):

```razor

        <GridEvents FilterDialogOpening="FilterDialogOpeningHandler"
                    Filtering="FilteringHandler"
                    TValue="GridRowViewModel"></GridEvents>

```


And the c# code:

```cs

    public void FilteringHandler(Syncfusion.Blazor.Grids.FilteringEventArgs args)
    {
        if (args.ColumnName == nameof(GridRowViewModel.Model)
            && args.FilterPredicates is not null
            && args.FilterPredicates.Any())
        {
            // Always include the first 1 model(s) (by SortOrder)
            var alwaysInclude = gridData
                .OrderBy(d => d.SortOrder)
                .Select(d => d.Model)
                .Distinct()
                .Take(1)
                .ToList();

            // Add alwaysInclude predicates
            foreach (var model in alwaysInclude)
            {
                // Is it not working because I can't set Uid (as it's internal set)?
                args.FilterPredicates.Add(new PredicateModel<object>
                {
                    Field = nameof(GridRowViewModel.Model),
                    Operator = Syncfusion.Blazor.Operator.Equal,
                    Value = model,
                    Predicate = "or", // "or" so any match will include the row
                    MatchCase = true,
                    IgnoreAccent = true
                });
            }
        }
    }

```


Is it not working because I can't set Uid in the PredicateModel as it is internal set property?


Is this the right direction I'm going? Or is there a better way? Should I just disable the filter choices (by greying them out) so those models can't be filtered out? If yes, how to do so?


Please suggest best practice way of doing this. Thanks!



AK Ashish Khanal July 15, 2025 12:00 PM UTC

Hello @syncfusion team,

Could someone please help me with this? Maybe a Senior Engineer on Data Grid team could help.

I'm stuck on this for days.

Thank you.



VN Vignesh Natarajan Syncfusion Team July 15, 2025 12:39 PM UTC

Hi Ashish, 

Sorry for the delay in getting back to you. 

We understand your requirement of maintain certain records in Grid when performing filtering action. As we do not have direct support to achieve your requirement, we are working on alternative way to achieve it. So, we need some more time to analyze the query and provide solution.


So we will update you further details by 17th July 2025. We appreciate your patience until then. 

Regards,

Vignesh Natarajan



AK Ashish Khanal July 15, 2025 03:03 PM UTC

Hi Vignesh,

Thank you for your response and no worries.

In addition to the requirements in my last message, could you suggest me a way to only show Model2Ord1 when all other models are un-selected?

Image_3194_1752590845356

For example, in above image, if I un-select Model1Ord2, Model4Ord3, and Model3Ord4, and somehow click Ok (currently the Ok button is disabled if I don't make any selection on the filter choices), I should be able to see  Model2Ord1 rows.

To summarize it:

- Select any model => show Model2Ord1 (always) + selected models

- Select no model => show Model2Ord1 (always)



NP Naveen Palanivel Syncfusion Team July 17, 2025 04:39 AM UTC

Hi Ashish,

Query 1 : A specific row should always be displayed in the grid, even when filters are applied. This additional record must remain visible regardless of the filtering conditions

We would like to inform you about the current behavior of our DataGrid filter. When selecting records using checkboxes and clicking the Filter button, the Filtered and Filtering events of the DataGrid will be triggered. In these events, you can retrieve the filtered data. Regarding the filter behavior: when more than half of the values in the list are filtered out, the filter applies an "AND" operator with a "not equal" predicate, which includes only the unchecked values provided in the event arguments. On the other hand, when less than half of the values are filtered out, the filter uses an "OR" operator with an "equal" predicate, which includes the checked values in the filter popup within the Filtering event arguments. This is the default behavior of the Grid and is designed to maintain stability.


Therefore, when filtering more than half of the records, you do not need to add any extra predicates. However, when filtering less than half of the records and you want to always display a specific row in the Grid, you can achieve this by using the Predicate object in the Columns property of GridFilterSettings. Please refer to the code snippet and sample provided below for implementation guidance.

Sample : https://blazorplayground.syncfusion.com/rXhIXwCmCyoJFZIj

  public async Task FilteringHandler(Syncfusion.Blazor.Grids.FilteringEventArgs args)

  {

      if (args.ColumnName == nameof(GridRowViewModel.Model)

          && args.FilterPredicates is not null

          && args.FilterPredicates.Any() && args.FilterPredicates[0].Operator != Syncfusion.Blazor.Operator.NotEqual)

      {

          // Always include the first 1 model(s) (by SortOrder)

          var alwaysInclude = gridData

              .OrderBy(d => d.SortOrder)

              .Select(d => d.Model)

              .Distinct()

              .Take(1)

              .ToList();

 

          // Add alwaysInclude predicates

          foreach (var model in alwaysInclude)

          {

              // Is it not working because I can't set Uid (as it's internal set)?

              args.FilterPredicates.Add(new PredicateModel<object>

                  {

                      Field = nameof(GridRowViewModel.Model),

                      Operator = Syncfusion.Blazor.Operator.Equal,

                      Value = model,

                      Predicate = "or", // "or" so any match will include the row

                      MatchCase = true,

                      IgnoreAccent = true

                  });

          }

 

          args.Cancel = true;

 

          string columnUid = args.FilterPredicates[0].Uid;

          foreach (var predicates in args.FilterPredicates)

          {

              if (Grid.FilterSettings.Columns == null)

              {

                  Grid.FilterSettings.Columns = new List<GridFilterColumn>();

              }

 

              Grid.FilterSettings.Columns.Add(new GridFilterColumn

                  {

                      Field = predicates.Field,

                      Operator = predicates.Operator,

                      Predicate = predicates.Predicate,

                      Value = predicates.Value,

                      Uid = columnUid

                  });

          }

          await Grid.Refresh();

      }

  }


Query: “When all checkboxes are unselected, you want to enable the OK button in the filter dialog.”


We have reviewed your additional requirement. It appears that you would like the OK button in the filter dialog to remain enabled even when all checkboxes are unselected. However, we regret to inform you that this is not currently supported at our end. The OK button in the filter dialog is only enabled when at least one value is selected in the filter checkbox list.

Regards,
Naveen



AK Ashish Khanal July 18, 2025 12:45 PM UTC

Hi Naveen Palanivel,

Thank you so much for your response!

I tried to use your code in my project, but I see few issues with it:


Issue 1: Filter menu loses choice when you select an option and go back to select other option

For example:

- Load the page here: https://blazorplayground.syncfusion.com/LthyNQWwGAohZqjB

- Select an option (for eg: select Model1Ord2) . This works as expected. ✅

- Now go back to filter menu to select Model4Ord3

- Now go back to filter menu again to select something else. At this point you lose your second selection Model4Ord3. As you can see Model4Ord3 is "de-selected" in the filter menu choices even though it's shown in the grid. This is unexpected. ⚠️🛑 


Issue 2: Visual Studio doesn't recommend this solution with its warnings saying I shouldn't be setting those properties. 

I'm doing this inside the component (Vendor.razor), but it still shows those warnings. So, is it a bad practice way of doing things? I'd rather always follow best practice way of doing things. If you have best practice way to achieve what I want, please recommend that over this.


Query: “When all checkboxes are unselected, you want to enable the OK button in the filter dialog.”

I'm trying to see if I can filter out all other models except the "always show" models in the grid. If I want to deselect (filter out) everything and only want to see "always show" model (Model2Ord1 in my case), how would you recommend I do that?


EDIT:

Basically, all I want is:

- Don't give user option to filter out "always show" models (For eg: Model2Ord1) by either not showing them in the filter menu choices or disabling them in the filter menu choices

- Select any model => show "always show" models (For eg: Model2Ord1) + selected models

- Select no model => show "always show" models (For eg: Model2Ord1)

Please give me the best practice way to achieve this requirement.


Thank you for your help. I really appreciate it!



AK Ashish Khanal July 18, 2025 01:25 PM UTC

Please check out my updated response under "Edit" section in my previous response.

What I want can be achieved by any of these options:

Option 1 (my preferred way):

Including always show models inside "Always Show Models" filter menu choice and disabling it so it can't be de-selected. It can't be de-selected even when you toggle "Select All".

Option 2:

Showing my always show models in the menu choices and simply disabling them so they can't be de-selected. They can't be de-selected even when you toggle "Select All".



NP Naveen Palanivel Syncfusion Team July 20, 2025 04:07 PM UTC

Hi Ashish,

Issue 1: Filter menu loses choice when you select an option and go back to select other option

We reviewed your issue and noticed that in the filter dialog, a checkbox becomes unchecked after being selected. This happens when you select an option, apply the filter, and then reopen the filter dialog to select another option. The lastly selected checkbox appears unselected, even though the filter still works correctly. We have modified the sample so that the checkbox remains selected. Please refer to the updated code snippet and sample for reference.

    public async Task FilteringHandler(Syncfusion.Blazor.Grids.FilteringEventArgs args)

    {

        if (args.ColumnName == nameof(GridRowViewModel.Model)

            && args.FilterPredicates is not null

            && args.FilterPredicates.Any() && args.FilterPredicates[0].Operator != Syncfusion.Blazor.Operator.NotEqual)

        {

            // Always include the first 1 model(s) (by SortOrder)

            var alwaysInclude = gridData

                .OrderBy(d => d.SortOrder)

                .Select(d => d.Model)

                .Distinct()

                .Take(1)

                .ToList();

 

            // Add alwaysInclude predicates

            foreach (var model in alwaysInclude)

            {

                // Is it not working because I can't set Uid (as it's internal set)?

                args.FilterPredicates.Add(new PredicateModel<object>

                    {

                        Field = nameof(GridRowViewModel.Model),

                        Operator = Syncfusion.Blazor.Operator.Equal,

                        Value = model,

                        Predicate = "or", // "or" so any match will include the row

                        MatchCase = true,

                        IgnoreAccent = true

                    });

            }

 

            args.Cancel = true;

 

 

            List<PredicateModel<object>> storedPredicates = args.FilterPredicates;

 

            await Grid.ClearFilteringAsync();

            string columnUid = args.FilterPredicates[0].Uid;

#pragma warning disable BL0005

            foreach (var predicates in storedPredicates)

            {

                if (Grid.FilterSettings.Columns == null)

                {

                    Grid.FilterSettings.Columns = new List<GridFilterColumn>();

                }

 

                Grid.FilterSettings.Columns.Add(new GridFilterColumn

                    {

                        Field = predicates.Field,

                        Operator = predicates.Operator,

                        Predicate = predicates.Predicate,

                        Value = predicates.Value,

                        Uid = columnUid

                    });

            }

#pragma warning restore BL0005

            await Grid.Refresh();

        }


Sample : https://blazorplayground.syncfusion.com/LtVIjGBtSojxJqSZ

Issue 2: Visual Studio doesn't recommend this solution with its warnings saying I shouldn't be setting those properties. 

We reviewed your query and understand that you are encountering the warning. This warning can be suppressed using the approach shown below. Please refer to the following code snippet for reference.

            List<PredicateModel<object>> storedPredicates = args.FilterPredicates;

 

            await Grid.ClearFilteringAsync();

            string columnUid = args.FilterPredicates[0].Uid;

#pragma warning disable BL0005

            foreach (var predicates in storedPredicates)

            {

                if (Grid.FilterSettings.Columns == null)

                {

                    Grid.FilterSettings.Columns = new List<GridFilterColumn>();

                }

 

                Grid.FilterSettings.Columns.Add(new GridFilterColumn

                    {

                        Field = predicates.Field,

                        Operator = predicates.Operator,

                        Predicate = predicates.Predicate,

                        Value = predicates.Value,

                        Uid = columnUid

                    });

            }

#pragma warning restore BL0005

            await Grid.Refresh();

        }


Issue 3: “When all checkboxes are unselected, you want to enable the OK button in the filter dialog.”

We are still evaluating the feasibility of your requirement on our end. We will provide you with an update on our findings within two days (by July 22nd , 2025). We appreciate your patience in the meantime.


Best regards,

Naveen



AK Ashish Khanal July 21, 2025 02:23 PM UTC

Hi Naveen,

Thank you for the response.

The below code works for:

- Select any model => show "always show" models (For eg: Model2Ord1) + selected models ✅✅

Thank you so much!

But I'm not comfortable disabling warnings as recommended in Blazor docs:

https://learn.microsoft.com/en-us/aspnet/core/diagnostics/bl0005?view=aspnetcore-9.0#when-to-suppress-warnings

So, is there a better way to do this? I'd rather do it using best practice way rather than suppress important warnings.


public async Task FilteringHandler(FilteringEventArgs args)
    {
        if (Grid is not null
            && args.ColumnName == nameof(GridRowViewModel.Model)
            && args.FilterPredicates is not null
            && args.FilterPredicates.Any()
            && args.FilterPredicates[0].Operator != Syncfusion.Blazor.Operator.NotEqual)
        {
            // Always include the first N models (by SortOrder)
            var alwaysInclude = gridData
                .OrderBy(d => d.SortOrder)
                .Select(d => d.Model)
                .Distinct()
                .Take(1)
                .ToList();

            // Add a predicate for the first N models (using "or" logic)
            foreach (var model in alwaysInclude)
            {
                args.FilterPredicates.Add(new PredicateModel<object>
                {
                    Field = nameof(GridRowViewModel.Model),
                    Operator = Syncfusion.Blazor.Operator.Equal,
                    Value = model,
                    Predicate = "or", // "or" so any match will include the row
                    MatchCase = true,
                    IgnoreAccent = true
                });
            }

            args.Cancel = true;
            await Grid.ClearFilteringAsync();

            string columnUid = args.FilterPredicates[0].Uid;
#pragma warning disable BL0005
            foreach (var predicate in args.FilterPredicates)
            {
                if (Grid.FilterSettings.Columns is null)
                {
                    Grid.FilterSettings.Columns = new List<GridFilterColumn>();
                }

                Grid.FilterSettings.Columns.Add(new GridFilterColumn
                {
                    Field = predicate.Field,
                    Operator = predicate.Operator,
                    Predicate = predicate.Predicate,
                    Value = predicate.Value,
                    Uid = columnUid
                });
            }
#pragma warning restore BL0005
            await Grid.Refresh();
        }
    }
----------------------------------------------------------------------------------------------------------------- As for the feature to:

- Select no model => show "always show" models (For eg: Model2Ord1)

Can you give me a way to include always show models inside "Always Show Models" filter menu choice and disable it so it can't be de-selected? It can't be de-selected even when you toggle "Select All". 

So "Ok" button will always be clickable. And this would allow me to see "always show" models (For eg: Model2Ord1) if I deselect all other models and hit "Ok".

Image_2334_1753107552207

This will satisfy all my requirements.

I care about clean, best practice solution, so please take your time to come up with a solution.

Thank you for your help!



NP Naveen Palanivel Syncfusion Team July 25, 2025 12:56 PM UTC

Hi Ashish Khanal,

We would like to inform you that the only available way to meet your requirement is by using the warning suppression code. This approach ensures that only one record remains in the grid, even after applying filters.


Regarding your other requirement—to keep the "Always Show Model" checkbox selected in the filter dialog, even when clicking "Select All" or "Unselect All"—we understand the uniqueness of this scenario. Unfortunately, we currently do not have built-in support for this specific functionality. We sincerely apologize for the inconvenience and appreciate your understanding.

Regards,
Naveen


Loader.
Up arrow icon