[PR #2290] [MERGED] RadzenDataGrid throws an exception when a filter value is loaded for a sub property #3174

Closed
opened 2026-01-29 18:22:15 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/radzenhq/radzen-blazor/pull/2290
Author: @Matt90hz
Created: 9/17/2025
Status: Merged
Merged: 9/22/2025
Merged by: @enchev

Base: masterHead: fix-load-sub-property-filter


📝 Commits (2)

📊 Changes

2 files changed (+176 additions, -5 deletions)

View changed files

📝 Radzen.Blazor.Tests/DataGridTests.cs (+151 -0)
📝 Radzen.Blazor/RadzenDataGrid.razor.cs (+25 -5)

📄 Description

Hi,
I have encountered this issue with RadzenDataGrid while storing and retrieving the settings from the browser data storage. The issue arise only when the settings are serialized an then de-serialized from JSON.

Example to reproduce the error

<ErrorBoundary @ref=_errorBoundary>
    <ChildContent>
        <TestDataGrid @ref=_dataGrid Data=_users AllowFiltering=true LoadSettings=OnLoadSettings SettingsChanged="OnSettingsChanged">
            <Columns>
                <RadzenDataGridColumn Title="Name" Property="Name" FilterMode="FilterMode.CheckBoxList" />
                <RadzenDataGridColumn Title="Roles" Property="Roles" FilterProperty="Id" Type=@typeof(IEnumerable<Role>) FilterMode="FilterMode.CheckBoxList">
                    <Template>
                        @string.Join(", ", context.Roles.Select(x => x.Description))
                    </Template>
                </RadzenDataGridColumn>
            </Columns>
        </TestDataGrid>
    </ChildContent>
    <ErrorContent>
        <RadzenText Text=@context.Message Style="color: red" />
        <RadzenButton Text="Recover" Click="OnRecoverClick" />
    </ErrorContent>
</ErrorBoundary>

<RadzenFieldset Text="Logs">
    @foreach (var log in _logs)
    {
        <RadzenText Text="@log" TextStyle="TextStyle.Body2" />
    }
</RadzenFieldset>

@using System.Text.Json
@using System.Collections
@code {
    record User(string Name, IEnumerable<Role> Roles);
    record Role(int Id, string Description);

    User[] _users = [
        new("Jhon", [new(0, "Admin")]),
        new("James", [
            new(0, "Admin"),
            new(1, "Guest")
        ]),
        new("Jhon", [new(1, "Guest")])
    ];

    static string? _settingsJson;
    RadzenDataGrid<User>? _dataGrid;
    ErrorBoundary? _errorBoundary;
    Stack<string> _logs = [];

    private void OnSettingsChanged(DataGridSettings args)
    {
        _settingsJson = JsonSerializer.Serialize(args, new JsonSerializerOptions()
        {
            WriteIndented = true
        });
        _logs.Push(_settingsJson);
        _logs.Push("Settings Changed");
    }

    private void OnLoadSettings(DataGridLoadSettingsEventArgs args)
    {
        if (_settingsJson is null) return;

        args.Settings = JsonSerializer.Deserialize<DataGridSettings>(_settingsJson);
        _logs.Push("Settings Loaded");
    }

    private void OnRecoverClick(MouseEventArgs args)
    {
        _settingsJson = null;
        _errorBoundary?.Recover();
        _logs.Push("Restored");
    }
}

Source of the problem

The exception is raised in this piece of code:

3dea0a5f67/Radzen.Blazor/RadzenDataGrid.razor.cs (L3696-L3710)

The first iteration detect a sub property, generate a List<Role> (for my example), and tries to populate it by calling GetFilterValue again on the elements of the array. This nested call will not find any match for the Role type and will end up returning a string that is not compatible with the List<Role>.

Fix

The fix that I have implemented detects if the grid column has a sub property filter, then adjusts the type that is passed to GetFilterValue to be an IEnumerable of the type of the sub property key. This then get correctly used by QueryExtensions to generate the expression to filter the data.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/radzenhq/radzen-blazor/pull/2290 **Author:** [@Matt90hz](https://github.com/Matt90hz) **Created:** 9/17/2025 **Status:** ✅ Merged **Merged:** 9/22/2025 **Merged by:** [@enchev](https://github.com/enchev) **Base:** `master` ← **Head:** `fix-load-sub-property-filter` --- ### 📝 Commits (2) - [`d858d15`](https://github.com/radzenhq/radzen-blazor/commit/d858d1517bc556847ac8f0ff338c37bfb20a7268) Add tests - [`cd2d903`](https://github.com/radzenhq/radzen-blazor/commit/cd2d903980b1b65e3b88f152965551b41633c239) Fix load settings from json ### 📊 Changes **2 files changed** (+176 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `Radzen.Blazor.Tests/DataGridTests.cs` (+151 -0) 📝 `Radzen.Blazor/RadzenDataGrid.razor.cs` (+25 -5) </details> ### 📄 Description Hi, I have encountered this issue with `RadzenDataGrid` while storing and retrieving the settings from the browser data storage. The issue arise only when the settings are serialized an then de-serialized from JSON. ## Example to reproduce the error ```CSharp <ErrorBoundary @ref=_errorBoundary> <ChildContent> <TestDataGrid @ref=_dataGrid Data=_users AllowFiltering=true LoadSettings=OnLoadSettings SettingsChanged="OnSettingsChanged"> <Columns> <RadzenDataGridColumn Title="Name" Property="Name" FilterMode="FilterMode.CheckBoxList" /> <RadzenDataGridColumn Title="Roles" Property="Roles" FilterProperty="Id" Type=@typeof(IEnumerable<Role>) FilterMode="FilterMode.CheckBoxList"> <Template> @string.Join(", ", context.Roles.Select(x => x.Description)) </Template> </RadzenDataGridColumn> </Columns> </TestDataGrid> </ChildContent> <ErrorContent> <RadzenText Text=@context.Message Style="color: red" /> <RadzenButton Text="Recover" Click="OnRecoverClick" /> </ErrorContent> </ErrorBoundary> <RadzenFieldset Text="Logs"> @foreach (var log in _logs) { <RadzenText Text="@log" TextStyle="TextStyle.Body2" /> } </RadzenFieldset> @using System.Text.Json @using System.Collections @code { record User(string Name, IEnumerable<Role> Roles); record Role(int Id, string Description); User[] _users = [ new("Jhon", [new(0, "Admin")]), new("James", [ new(0, "Admin"), new(1, "Guest") ]), new("Jhon", [new(1, "Guest")]) ]; static string? _settingsJson; RadzenDataGrid<User>? _dataGrid; ErrorBoundary? _errorBoundary; Stack<string> _logs = []; private void OnSettingsChanged(DataGridSettings args) { _settingsJson = JsonSerializer.Serialize(args, new JsonSerializerOptions() { WriteIndented = true }); _logs.Push(_settingsJson); _logs.Push("Settings Changed"); } private void OnLoadSettings(DataGridLoadSettingsEventArgs args) { if (_settingsJson is null) return; args.Settings = JsonSerializer.Deserialize<DataGridSettings>(_settingsJson); _logs.Push("Settings Loaded"); } private void OnRecoverClick(MouseEventArgs args) { _settingsJson = null; _errorBoundary?.Recover(); _logs.Push("Restored"); } } ``` ## Source of the problem The exception is raised in this piece of code: https://github.com/radzenhq/radzen-blazor/blob/3dea0a5f67cb0c50fcdeee83dbac61daa486520b/Radzen.Blazor/RadzenDataGrid.razor.cs#L3696-L3710 The first iteration detect a sub property, generate a `List<Role>` (for my example), and tries to populate it by calling GetFilterValue again on the elements of the array. This nested call will not find any match for the Role type and will end up returning a string that is not compatible with the `List<Role>`. ## Fix The fix that I have implemented detects if the grid column has a sub property filter, then adjusts the type that is passed to `GetFilterValue` to be an `IEnumerable` of the type of the sub property key. This then get correctly used by `QueryExtensions` to generate the expression to filter the data. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 18:22:15 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#3174