DataGrid Filter Ignores Filter Operator Set Programmatically

I have a DataGrid with a simple filter setup. For one of the columns, I need to use a custom filter template, allowing users to query for IsEmpty records. I am programmatically changing the filter operator for that column.

However, the filter retrieved from the LoadData method ignores the IsEmpty condition and defaults to Contains. How can I resolve this issue?

builder.OpenComponent<RadzenDataGridColumn<T>>(0 + i * 41);
.
.
builder.AddAttribute(1 + i * 41, "Property", colDef.Property);
.
.
builder.AddAttribute(9 + i * 41, "FilterOperator", colDef.FilterOperator);

I'm afraid that the code provided is not enough to provide any advice. You can attach Radzen.Blazor.csproj to your solution to debug your case.

Hi @enchev ,

I think I reproduced it with your demo page.

<RadzenDataGrid style="height: 335px" @ref="grid" FilterMode="FilterMode.Simple" IsLoading=@isLoading Count="@count" Data="@employees" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="4" PagerHorizontalAlign="HorizontalAlign.Center" ColumnWidth="200px">
    <Columns>
        <RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>
        <RadzenDataGridColumn Property="@nameof(Employee.LastName)" Title="Last Name" Width="160px"/>
    </Columns>
</RadzenDataGrid>

@code {
    async Task LoadData(LoadDataArgs args)
    {
        if (!string.IsNullOrEmpty(args.Filter))
        {
            filterTerm = args.Filter;
        }
    }
}

I changed FilterOperator in FirstName column. When I add filter to LastName, FirstName IsEmpty is ignored.

Which page? This code is incomplete and cannot be used as is.

Sorry for that.. This is the full code that I have edited from datagrid-loaddata

@using System.Linq.Dynamic.Core
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind

@inherits DbContextPage

<RadzenButton Text="Reset" Click="@Reset" Style="margin-bottom: 20px;" />
<RadzenDataGrid style="height: 335px" @ref="grid" FilterMode="FilterMode.Simple" IsLoading=@isLoading Count="@count" Data="@employees" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="4" PagerHorizontalAlign="HorizontalAlign.Center" ColumnWidth="200px">
    <Columns>
        <RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>
        <RadzenDataGridColumn Property="@nameof(Employee.LastName)" Title="Last Name" Width="160px"/>
    </Columns>
</RadzenDataGrid>

<RadzenCard class="rz-mt-6">
    <RadzenText TextStyle="TextStyle.Body1">
        @filterTerm
    </RadzenText>
</RadzenCard>

@code {
    RadzenDataGrid<Employee> grid;
    int count;
    IEnumerable<Employee> employees;
    bool isLoading = false;

    string filterTerm = "";

    List<string> titles = new List<string> {"Sales Representative", "Vice President, Sales", "Sales Manager", "Inside Sales Coordinator" };
    IEnumerable<string> selectedTitles;

    async Task OnSelectedTitlesChange(object value)
    {
        if (selectedTitles != null && !selectedTitles.Any())
        {
            selectedTitles = null;  
        }
        
        await grid.FirstPage();
    }

    async Task Reset()
    {
        grid.Reset(true); 
        await grid.FirstPage(true);
    }

    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;

        await Task.Yield();

        // This demo is using https://dynamic-linq.net
        var query = dbContext.Employees.AsQueryable();

        if (!string.IsNullOrEmpty(args.Filter))
        {
            filterTerm = args.Filter;
            // Filter via the Where method
            query = query.Where(args.Filter);
        }

        if (!string.IsNullOrEmpty(args.OrderBy))
        {
            // Sort via the OrderBy method
            query = query.OrderBy(args.OrderBy);
        }

        // Important!!! Make sure the Count property of RadzenDataGrid is set.
        count = query.Count();

        // Perform paging via Skip and Take.
        employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();

        isLoading = false;
    }
}

This is what I see in our demo using this code:

The first column filter expression is part of the filtering.

Please try to reset and add filter...

You've intentionally reset the column state including filtering.

Yes, In my case I have to change filter operator programmatically. Same thing happening there.

protected void OnFilterSetByTemplate()
{

    var column = GridSettings.columnDefinitions.FirstOrDefault(c => c.Property.Equals(Template.Prop);

    if (column != null)
    {
        column.FilterValue = Template.FilterValue;
		column.FilterOperator = Template.FilterOperator


		StateHasChanged();
        Grid.Reload();
    }    
}

I think when we call grid.Reset(true), It is need to reset to the filter operator that we set on the Blazor component.

in the above example FirstName filter operator needs to reset to FilterOperator.IsEmpty

<RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>

As I already noted Reset(true) and Reset() will reset all columns state including filtering. If you do not want that you can use Reset(false) or set desired settings to the column after Reset().