Is it feasible to implement Save/Load settings on Dynamic DataGrid? I ran into this error. Thanks in advance.
There are nested double quotes in this case for column Property - you need to escape the value.
1 Like
Thank you very much! The below fixed the issue.

I feel there should be a better solution than the above but it's working fine. I was able to save all kinds of settings just fine (column hiding, filtering, sorting, column reorder, column resize). If anyone happens to have a more elegant solution, please do post it here. Thanks!
Ran into this thread because I was having a similar issue, I've solved it in a IMO more elegant way.
Create two extension methods in a public static class:
public static string EncodeForCookie(this string value)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
return WebEncoders.Base64UrlEncode(bytes);
}
public static string DecodeFromCookie(this string value)
{
byte[] bytes = WebEncoders.Base64UrlDecode(value);
return System.Text.Encoding.UTF8.GetString(bytes);
}
and then in your save and load methods:
private async Task LoadStateAsync()
{
await Task.CompletedTask;
var result = await _jsRuntime.InvokeAsync<string>("window.localStorage.getItem", "Settings");
if (!string.IsNullOrEmpty(result))
{
var json = result.DecodeFromCookie();
_settings = JsonSerializer.Deserialize<DataGridSettings>(json)!;
}
}
private async Task SaveStateAsync()
{
await Task.CompletedTask;
var settings = JsonSerializer.Serialize(Settings).EncodeForCookie();
await _jsRuntime.InvokeVoidAsync("eval", $"window.localStorage.setItem('Settings', '{settings}')");
}
1 Like
