How to reset Virtualized DataGrid after filtering

When I scroll the virtualized grid, far enough, and I apply a filter from a column filter, the datagrid is not reset properly and i can't see the results.

You can use the code at the bottom of the page with this address Blazor DataGrid custom virtualization

Scenario 1 : Scroll to the middle of the virtual grid, then apply filter on OrderID with 10249. It displays "No records to display"

Scenario 2 : Reset the page, scroll to middle of virtual grid, use search box to filter with 10249. Filtering succeeded because OnSearchAsync use grid.Reload()

Question : is it possible to reset the grid after applying template for column? How to reset the grid if I use grid.RefreshDataAsync() instead of reload?

Thank you for your help.

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

@inherits DbContextPage

<RadzenButton Text="Refresh" Click="@(args => grid.Reload())" class="my-3" />
<br />
Search<RadzenTextBox Text="Search" ValueChanged="OnSearchAsync" />

<RadzenDataGrid @ref=grid
    Data="@orderDetails"
    Count="@count"
    LoadData="@LoadData"
    TItem="OrderDetail"
    AllowVirtualization="true"
    Style="height:400px"
    AllowFiltering="true"
    FilterPopupRenderMode="PopupRenderMode.OnDemand"
    FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
    LogicalFilterOperator="LogicalFilterOperator.Or"
    AllowSorting="true">

    <Columns>
        <RadzenDataGridColumn TItem="OrderDetail" Property="OrderID" Title="OrderID" />
        <RadzenDataGridColumn TItem="OrderDetail" Property="ProductID" Title="ProductID" />
        <RadzenDataGridColumn TItem="OrderDetail" Property="UnitPrice" Title="Unit Price">
            <Template Context="detail">
                @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
        <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount">
            <Template Context="detail">
                @String.Format("{0}%", detail.Discount * 100)
            </Template>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

<EventConsole @ref=@console />
@code {
    EventConsole console;
    RadzenDataGrid<OrderDetail> grid;
    int count;
    IEnumerable<OrderDetail> orderDetails;
    private string? _searchText;

	private async Task OnSearchAsync(string search)
	{
		_searchText = string.IsNullOrWhiteSpace(search) || grid is null
			? null
			: search;

        console.Log("OnSearch");
		await grid!.Reload();
        <!-- await grid!.RefreshDataAsync(); -->
	}


    void LoadData(LoadDataArgs args)
    {
        console.Log($"Skip: {args.Skip}, Top: {args.Top}");

        var query = dbContext.OrderDetails.AsQueryable();

        if (!string.IsNullOrEmpty(args.Filter))
        {
            console.Log($"{args.Filter}");
            query = query.Where(args.Filter);
        }
        if (!string.IsNullOrEmpty(_searchText))
        {
            console.Log($"Search : {_searchText}");
            query = query.Where("OrderID == 10248");
        }
        if (!string.IsNullOrEmpty(args.Filter) || !string.IsNullOrEmpty(_searchText))
        {
            console.Log("Query.Count()");
            count = query.Count();
        }
        else
        {
            count = dbContext.OrderDetails.Count();
        }

        if (!string.IsNullOrEmpty(args.OrderBy))
        {
            query = query.OrderBy(args.OrderBy);
        }

        // Add more rows
        var t = Enumerable.Concat(query.ToList(), query.ToList()).ToList();
        t = Enumerable.Concat(t, query.ToList()).ToList();
        
        var myCount = t.Count();
        console.Log($"{myCount}");


        orderDetails = t.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
    }
}

I've updated the demo to handle better filtering in such cases and we will publish our demos later today. Here is the relevant code:

string lastfilter;
void LoadData(LoadDataArgs args)
{
    if(!string.IsNullOrEmpty(args.Filter) && lastfilter != args.Filter)
    {
        args.Skip = 0;
    }

    console.Log($"Skip: {args.Skip}, Top: {args.Top}");

    var query = dbContext.OrderDetails.AsQueryable();

    if (!string.IsNullOrEmpty(args.Filter))
    {
        lastfilter = args.Filter;
        query = query.Where(args.Filter);
        count = query.Count();
    }
    else
    {
        count = dbContext.OrderDetails.Count();
    }

    if (!string.IsNullOrEmpty(args.OrderBy))
    {
        query = query.OrderBy(args.OrderBy);
    }

    orderDetails = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
}

Hey Enchev, thank you for your quick response.

I checked the new code on website and the behavior has not changed : when I don't have filter, I scroll to middle of page, then I apply filter and it still shows "No record".

Am I missing something?

Weโ€™ve still not deployed the update to our website howe you can pull the code from GitHub and check/run locally.

Hi Enchev, do you have an approximative time for when the correction will be available in your web site?

Thank you!

It will be published before the end of this week. Please follow my suggestion in my previous reply.

1 Like

Ok I tried the code and it's working. Thank you.

Is there a way to reset the scrollbar to the top of the grid after calling RefreshData? It's for when we use a filter not included in the grid. When applying the filter, we call RefreshDataAsync and the scrollbar is not reseting to the top.

This code should reset the scrollbar to top if you call Reload() method.

Hi Enchev, si there a way to reset the scrollbar to top when call RefreshDataAsync() instead of Reload? We're having a problem in one of our grids. When using Reload() the grid does not refresh properly. When using RefreshDataAsync() the data are refreshed correctly but the scrollbar is not going back to top.

Any idea?

This method will just call the method of the Virtualize component used internally and will clear some cache. radzen-blazor/Radzen.Blazor/RadzenDataGrid.razor.cs at master ยท radzenhq/radzen-blazor ยท GitHub

Reload() will definitely reset to the top the scrollbars:
dg-virt-loaddata-filter

1 Like

Hi Enchev, I saw the site was updated but there is still a problem

  1. Scroll couple pages
  2. Set Filter OrderId to 10248
  3. Set Filter OrderId to 10249 without clearing filter
  4. Now, Clear Filters
  5. Set Filter OrderId to 10249
  6. No records to display

Radzen-DataGrid

Hi @philippedurocher,

You can adapt the code of the demo to work in the way you want. We accept also pull requests.