DataGrid Settings and OnRender Grouping Conflict

Hi,

I'm running into an issue when combining the Radzen DataGrid 's settings persistence (Save/Load settings) with grouping via the OnRender event.

Example

void OnRender(DataGridRenderEventArgs<Order> args)
{
    if (args.FirstRender)
    {
        args.Grid.Groups.Add(new GroupDescriptor() 
        { 
            Property = "Employee.LastName", 
            Title = "Employee" 
        });

        StateHasChanged();
    }
}

The Problem:
When using this method to group by "Employee.LastName" during the first render, it causes the grid settings stored in localStorage to be overwritten/reset — effectively discarding any previously saved user settings.

I initially thought I could fix this by checking if the grouping already exists in the current settings:

“If "Employee.LastName" already exists in settings, do nothing — else, add it.”

However, there's a timing issue:

  • The OnRender method is triggered before LoadStateAsync has finished loading the saved settings.
  • So the grouping check is always false, and the grouping is added again → overwriting the saved state.

Has anyone found a reliable way to:

  • Add grouping dynamically via OnRender, only if it's not already saved,
  • without overwriting existing DataGrid settings?

In my opinion you should use OnAfterRenderAsync() override for both operations instead Render callback of the DataGrid. First you can load the settings and then check if grouping should be added.

1 Like

@enchev thank you for pointing to the right direction! Solved.

First of all, removed the OnRender option and then added the following option to the datagrid

@ref="@releaseNoteDataGrid"

In @code block:

adzenDataGrid<Note> releaseNoteDataGrid;

OnAfterRenderAsync

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        // Get settings from Local Storage
        await LoadStateAsync();

        // Add predefined grouping if no settings
        if (_settings == null)
        {
            releaseNoteDataGrid.Groups.Add(new GroupDescriptor() { Property = "releaseDate", SortOrder = SortOrder.Descending, Title = "Release Date" });
        }

        StateHasChanged();
    }
}