RadzenDataGrid: Make the Render callback async safe #987

Closed
opened 2026-01-29 17:47:21 +00:00 by claunia · 2 comments
Owner

Originally created by @Daeymon on GitHub (Sep 19, 2023).

The current Render callback is an Action<DataGridRenderEventArgs<TItem>> which makes it synchronous. This limits the usage of the callback, in that async methods can not be safety invoked within the callback.

Now that seems a shame and a missed opportunity since the callback is invoked within an asynchronous method (OnAfterRenderAsync). Because its invoked within an async method, there is the opportunity to enhance this callback with an async safe type and that is to change Render to a EventCallback<DataGridRenderEventArgs<TItem>>.

So instead of:

if (Render != null)
{
    Render(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender });
}

you would have:

if (Render.HasDelegate)
{
    await Render.InvokeAsync(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender });
}

This option would support synchronous and asynchronous methods for the Render callback.

Originally created by @Daeymon on GitHub (Sep 19, 2023). The current Render callback is an `Action<DataGridRenderEventArgs<TItem>>` which makes it synchronous. This limits the usage of the callback, in that async methods can not be safety invoked within the callback. Now that seems a shame and a missed opportunity since the callback is invoked within an asynchronous method (OnAfterRenderAsync). Because its invoked within an async method, there is the opportunity to enhance this callback with an async safe type and that is to change Render to a `EventCallback<DataGridRenderEventArgs<TItem>>`. So instead of: ``` if (Render != null) { Render(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender }); } ``` you would have: ``` if (Render.HasDelegate) { await Render.InvokeAsync(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender }); } ``` This option would support synchronous and asynchronous methods for the Render callback.
Author
Owner

@enchev commented on GitHub (Sep 19, 2023):

Have you tried your suggestion? This will cause immediately render loop since EventCallback will invoke StateHasChanged() automatically.

@enchev commented on GitHub (Sep 19, 2023): Have you tried your suggestion? This will cause immediately render loop since EventCallback will invoke StateHasChanged() automatically.
Author
Owner

@Daeymon commented on GitHub (Sep 19, 2023):

Of course it will. I had totally forgotten about that. Okay, an alternative suggestion. Add a new parameter offering an async equivalent:

[Parameter]
public Func<DataGridRenderEventArgs<TItem>, Task> RenderAsync { get; set; }

then add straight after:

if (Render != null)
{
    Render(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender });
}

the following

if (RenderAsync != null)
{
    await RenderAsync(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender });
}
@Daeymon commented on GitHub (Sep 19, 2023): Of course it will. I had totally forgotten about that. Okay, an alternative suggestion. Add a new parameter offering an async equivalent: ``` [Parameter] public Func<DataGridRenderEventArgs<TItem>, Task> RenderAsync { get; set; } ``` then add straight after: ``` if (Render != null) { Render(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender }); } ``` the following ``` if (RenderAsync != null) { await RenderAsync(new Radzen.DataGridRenderEventArgs<TItem>() { Grid = this, FirstRender = firstRender }); } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#987