Serialize/deserialize DataGridSettings with custom filters fails

I'm trying to persist the grid state (visible columns, sorting orders, filters, ...) to later retrieve this state and restore the grid. However, when using a custom filter (RadzenDropDown) the DataGridSettings are not restored to the the original value. Mainly the FilterValue type on the column with the custom filter is changed, which results in an incorrect OData request.

See the information and a small example project attached to this post. (The solution is called SfPlayground because we first used Syncfusion Blazor components, but we want to migrate to Radzen Blazor components.)

I've tried Newtonsoft.JSON with TypeNameAssemblyFormatHandling to get a typed JSON, but with no success.

Any ideas of how to achieve my goal?

-- Niels


DataGridSettings after selecting a filter (values: 1, 2) on the PostId column. Type is System.Linq.EnumerableQuery<int>.

DataGridSettings after deserializing. Type is System.Text.Json.JsonElement.

This is the JSON produced when serializing DataGridSettings:

{
    "Columns":
    [
        {
            "Property": "Id",
            "Visible": true,
            "Width": null,
            "OrderIndex": null,
            "SortOrder": null,
            "FilterValue": null,
            "FilterOperator": 0,
            "SecondFilterValue": null,
            "SecondFilterOperator": 0,
            "LogicalFilterOperator": 0
        },
        {
            "Property": "PostId",
            "Visible": true,
            "Width": null,
            "OrderIndex": null,
            "SortOrder": null,
            "FilterValue":
            [
                1,
                2
            ],
            "FilterOperator": 6,
            "SecondFilterValue": null,
            "SecondFilterOperator": 0,
            "LogicalFilterOperator": 1
        },
        {
            "Property": "Name",
            "Visible": true,
            "Width": null,
            "OrderIndex": null,
            "SortOrder": null,
            "FilterValue": null,
            "FilterOperator": 6,
            "SecondFilterValue": null,
            "SecondFilterOperator": 0,
            "LogicalFilterOperator": 0
        },
        {
            "Property": "Email",
            "Visible": true,
            "Width": null,
            "OrderIndex": null,
            "SortOrder": null,
            "FilterValue": null,
            "FilterOperator": 6,
            "SecondFilterValue": null,
            "SecondFilterOperator": 0,
            "LogicalFilterOperator": 0
        }
    ],
    "Groups":
    [],
    "CurrentPage": 0,
    "PageSize": 50
}

SfPlayground.zip (451.4 KB)

I've added column with custom filtering to our Save/Load settings demo however everything worked normally:


datagrid-customfilter-savesettings

You can attach Radzen.Blazor project source code to your app to debug your case.

<p>@_settings?.Columns?.ToArray()[1].FilterValue.GetType()</p> gives System.Text.Json.JsonElement as a result. This doesn't seem to be an issue if you're using a ICollection or similar.

However, we're using OData and we rely on the args.Filter property to build the OData URI (cfr. your examples).

I've used the following code snippet to restore the Column.FilterValue as an IEnumerable and this fixes our problem.

_deserializedGridSettings = JsonSerializer.Deserialize<DataGridSettings>(_serializedGridSettings);

foreach (var column in _deserializedGridSettings!.Columns.Where(x => x.FilterValue is JsonElement))
{
    column.FilterValue = column.FilterValue.ToString()?.Trim('[', ']').Split(",").Select(x => x.Trim('"'));
}
        
_gridSettings = _deserializedGridSettings;

-- Niels

It's only a partial solution. The data is now filtered correctly, but the associated backing properties (in your example selectedTitles) are not updated.

-- Niels