RadzenDataGrid displays incorrectly ordered data when using async LoadData #1104

Closed
opened 2026-01-29 17:49:00 +00:00 by claunia · 1 comment
Owner

Originally created by @egans146 on GitHub (Jan 25, 2024).

Describe the bug
This issue arises when using asynchronous LoadData callbacks and AllowSorting=true.
In the example below, the data displayed is not ordered according to the selected sort order :

<RadzenDataGrid @ref=grid AllowVirtualization=true
                LoadData="OnGridLoadData"
                Data="gridItems"
                Count=gridItemCount
                Style="height: 30rem;"
                AllowSorting=true>
    <Columns>
        <RadzenDataGridColumn TItem="Item" Property="@nameof(Item.Value)" Title="Value" />
    </Columns>
</RadzenDataGrid>

<RadzenButton Click="() => grid.RefreshDataAsync()">Refresh</RadzenButton>

@code {
    class Item(int value) { public int Value { get; } = value; };

    RadzenDataGrid<Item> grid;

    int gridItemCount = 1000;

    IReadOnlyList<Item> itemSource;

    protected override void OnInitialized()
    {
        var items = Enumerable.Range(0, gridItemCount).Select(e => new Item(e)).ToArray();
        Random.Shared.Shuffle(items);
        itemSource = items;
    }

    IEnumerable<Item> gridItems;

    async Task OnGridLoadData(LoadDataArgs args)
    {
        await Task.Delay(Random.Shared.Next(60, 110));

        IEnumerable<Item> query = itemSource;

        var sortOrder = args.Sorts?.FirstOrDefault()?.SortOrder;

        if (sortOrder != null)
        {
            if (sortOrder == SortOrder.Ascending)
                query = query.OrderBy(e => e.Value);
            else
                query = query.OrderByDescending(e => e.Value);
        }

        gridItems = query.Skip(args.Skip ?? 0).Take(args.Top ?? 10).ToArray();
        StateHasChanged();
    }
}

Animation

Expected behavior
RadzenDataGrid should display data sorted by the current sort order.

Desktop:

  • OS: Windows
  • Browser: Brave
  • Version 4.24.0
Originally created by @egans146 on GitHub (Jan 25, 2024). <!-- IMPORTANT: Read this first!!! 1. If you own a Radzen Professional or Еnterprise subscription you can report your issue or ask us a question via email at info@radzen.com. Radzen staff will reply within 24 hours (Professional) or 16 hours (Enterprise) 2. The Radzen staff guarantees a response to issues in this repo only to paid subscribers. 3. If you have a HOW TO question start a new forum thread in the Radzen Community forum: https://forum.radzen.com. Radzen staff will close issues that are HOWTO questions. 4. Please adhere to the issue template. Specify all the steps required to reproduce the issue or link a project which reproduces it easily (without requiring extra steps such as restoring a database). --> **Describe the bug** This issue arises when using asynchronous LoadData callbacks and AllowSorting=true. In the example below, the data displayed is not ordered according to the selected sort order : ```razor <RadzenDataGrid @ref=grid AllowVirtualization=true LoadData="OnGridLoadData" Data="gridItems" Count=gridItemCount Style="height: 30rem;" AllowSorting=true> <Columns> <RadzenDataGridColumn TItem="Item" Property="@nameof(Item.Value)" Title="Value" /> </Columns> </RadzenDataGrid> <RadzenButton Click="() => grid.RefreshDataAsync()">Refresh</RadzenButton> @code { class Item(int value) { public int Value { get; } = value; }; RadzenDataGrid<Item> grid; int gridItemCount = 1000; IReadOnlyList<Item> itemSource; protected override void OnInitialized() { var items = Enumerable.Range(0, gridItemCount).Select(e => new Item(e)).ToArray(); Random.Shared.Shuffle(items); itemSource = items; } IEnumerable<Item> gridItems; async Task OnGridLoadData(LoadDataArgs args) { await Task.Delay(Random.Shared.Next(60, 110)); IEnumerable<Item> query = itemSource; var sortOrder = args.Sorts?.FirstOrDefault()?.SortOrder; if (sortOrder != null) { if (sortOrder == SortOrder.Ascending) query = query.OrderBy(e => e.Value); else query = query.OrderByDescending(e => e.Value); } gridItems = query.Skip(args.Skip ?? 0).Take(args.Top ?? 10).ToArray(); StateHasChanged(); } } ``` ![Animation](https://github.com/radzenhq/radzen-blazor/assets/6442594/964f546f-3743-4e1c-b5a5-655a6861e0d8) **Expected behavior** RadzenDataGrid should display data sorted by the current sort order. **Desktop:** - OS: Windows - Browser: Brave - Version 4.24.0
Author
Owner

@enchev commented on GitHub (Jan 26, 2024):

The issue again is caused by the Virtualize component - it's not refreshed even after StateHasChanged() call. Unfortunately I don't have ideas on how to fix this and the only option I can offer you is to force refresh if needed using RefreshDataAsync() method:

async Task OnGridLoadData(LoadDataArgs args)
{
    await Task.Delay(Random.Shared.Next(60, 110));

    IEnumerable<Item> query = itemSource;

    var sortOrder = args.Sorts?.FirstOrDefault()?.SortOrder;

    if (sortOrder != null)
    {
        if (sortOrder == SortOrder.Ascending)
            query = query.OrderBy(e => e.Value);
        else
            query = query.OrderByDescending(e => e.Value);
    }

    gridItems = query.Skip(args.Skip ?? 0).Take(args.Top ?? 10).ToArray();
    gridItemCount = query.Count();

    if (grid.View.FirstOrDefault() != gridItems.FirstOrDefault())
    {
        await grid.RefreshDataAsync();
    }
}
@enchev commented on GitHub (Jan 26, 2024): The issue again is caused by the Virtualize component - it's not refreshed even after StateHasChanged() call. Unfortunately I don't have ideas on how to fix this and the only option I can offer you is to force refresh if needed using `RefreshDataAsync()` method: ``` async Task OnGridLoadData(LoadDataArgs args) { await Task.Delay(Random.Shared.Next(60, 110)); IEnumerable<Item> query = itemSource; var sortOrder = args.Sorts?.FirstOrDefault()?.SortOrder; if (sortOrder != null) { if (sortOrder == SortOrder.Ascending) query = query.OrderBy(e => e.Value); else query = query.OrderByDescending(e => e.Value); } gridItems = query.Skip(args.Skip ?? 0).Take(args.Top ?? 10).ToArray(); gridItemCount = query.Count(); if (grid.View.FirstOrDefault() != gridItems.FirstOrDefault()) { await grid.RefreshDataAsync(); } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#1104