Apply filter by code to RadzenDataGrid

Hello everyone. I'm having a problem applying filters to a RadzenDataGrid from my code. I have the component declared as follows:

<RadzenDataGrid  
                    AllowFiltering="true" 
                    AllowColumnResize="true" 
                    FilterMode="FilterMode.Advanced" 
                    FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" 
                    AllowSorting="true" 
                    PageSize="12" 
                    AllowPaging="true" 
                    PagerHorizontalAlign="HorizontalAlign.Left" 
                    ShowPagingSummary="true"
                    Data="@CustomFiltered" 
                    TItem="Imputation" 
                    ColumnWidth="300px" 
                    LogicalFilterOperator="LogicalFilterOperator.And" 
                    @ref="imputationsGrid">

In code I have declared the CustomFiltered variable as follows:

public List<Imputation> CustomFiltered{ get; set; }

and in the OnInitializedAsync function I query the database and load the results into that list.

The problem I have is when I try to apply a filter to that table after having loaded everything (by code). I have a button that calls a function that does the following:

imputationsGrid.Query.Filter = gridFilterSave.QueryFilter; // ((np(EmployeeOwner.Name)) == null ? "" : (np(EmployeeOwner.Name))).ToLower().Contains("test".ToLower()) and ((np(UsingProject.Name)) == null ? "" : (np(UsingProject.Name))).ToLower() == "project".ToLower()
imputationsGrid.Query.OrderBy = gridFilterSave.QueryOrder; //hours asc
imputationsGrid.Query.Skip = 0;
imputationsGrid.Reload();

When finished, the table is not sorted by the indicated column and filters are not applied. Also, if I display the column filters they appear empty.

image

In case it's important, the project uses NET 5 and the Radzen.Blazord(3.17.2) libraries.

Can you tell me why I can't apply the filter this way? Should it be done in a different way?

Greetings and thank you.

I don't think that assigning values to the Query property of the grid instance will work. I suggest checking the Filter API demo instead: Blazor DataGrid Filter API

That has a drawback. If I want to filter a "date" column, I can, from the grid filter, insert a "from" and "to" values.
However, if I do it as the link says, the column only has one FilterValue property, so I can't set a double filter on the same column.

There is a SecondFilterValue property as well.

Sorry, I had not seen that property. But I still have a doubt, is it possible to convert the string that is stored in grid.Query.Filter to a readable structure to set the values as indicated in that link?

I don't understand your question. What exactly are you trying to achieve?

When I save a filter to the database, I access grid.Query.Filter and store a string in this format:

Day > DateTime("2022-09-01") and ((np(EmployeeOwner.Name)) == null ? "" : (np(EmployeeOwner.Name))).ToLower().Contains("test".ToLower ())

I do this because, initially, the columns have empty FilterValue and SecondFilterValue properties. When the user sets a filter, and I access those properties, they are still empty, so I can only get the filter with grid.Query.Filter to store it.

When the user wants to reset a saved filter, according to the documentation, I have to loop through the grid columns and set the FilterValue and SecondFilterValue properties. For that I would need some way to parse the string that I indicated above to be able to establish those columns.

I do not know if there is another way to access the filter established in each column when storing said filter in the database or if there are other properties that store said filter.

Hey @Alejandro_Pinto_Rodr ,

This is not the proper way to save/restore data grid filtering. Check this demo for reference:

Furthermore DataGridSettings can be serialized and deserialized from/to JSON:

1 Like

I have tried what you tell me but it never sets the Settings variable, so it never executes the SaveStateAsync function.

It also tells me that the DataGridSettings class is not found and, for now, I have changed it to object.

This feature has been released today. Make sure you are using the latest Radzen.Blazor version.

It's perfect, exactly what I need but I found a "bug".

In some columns I have defined a FilterTemplate to customize it. Those custom filters, when selecting a value, do not set the bind-settings variable.

Do I have to add some change function or something similar to my custom filter so that it updates the bind-settings variable?

If you are working with column FilterValue and FilterOperator properties, DataGridSettings will be updated properly.

That is not what I mean. When I use a custom filter on a column, for example, a dropdown, and I select a value on the web in that dropdown, the variable that is assigned in bind-settings is not set.

When I select that custom dropdown in the projects filter, the table is correctly filtered but the variable assigned in bind-settings is not set.

I’ll repeat my previous reply - there is now way to save anything if you don’t use these properties. Check this demo for reference:

Ok, this is exactly what I was looking for. Thank you.

I had started with the Filter API example (mentioned in a comment above), but Blazor doesn't permit modifying component params directly, so I was having to set up an object that my Column referenced, and...it got ugly quickly. This is much better.