Hello, i have a problem with the RadzenDatagrid. There are many similar requests here in the forum, but I haven't found a solution that works for me yet.
It's about manually updating the datagrid (e.g. after adding data through a user action). The datagrid has no filters, the data is provided via the LoadData event. I've reproduced this using a sample project (in the original, the data source is a REST API).
In the initial state, the datagrid is empty. I now create new elements in the data source using the Create button and want to update the display using Datagrid.Relaod(). So far, without success, even a StateHasChanged() doesn't produce any results.
So how do I get the datagrid to call the LoadData event? By the way, an initially existing data source is displayed correctly, but even then the problem is that changes are not recognized.
The code looks like this:
@page "/start"
<PageTitle>Start</PageTitle>
<RadzenRow
Gap="5"
RowGap="5">
<RadzenColumn Size="12">
<RadzenStack Orientation="Orientation.Horizontal" Gap="16px" JustifyContent="JustifyContent.Start">
<RadzenButton
Click="@(_ => OnCreateButtonClick())"
Text="Create 20 Ids"
ButtonStyle="ButtonStyle.Primary"/>
<RadzenButton
Click="@(_ => OnClearButtonClick())"
Text="Delete All"
ButtonStyle="ButtonStyle.Base"/>
</RadzenStack>
</RadzenColumn>
<RadzenColumn>
<RadzenDataGrid
class="rz-border-start-0 rz-border-end-0"
ref="@_dataGrid"
Data="@(_filteredGuids)"
Count="@_countAll"
LoadData="@LoadData"
AllowVirtualization="true"
SelectionMode="DataGridSelectionMode.Single"
@bind-Value="@_selectedIds"
Style="width: 100%; height: 400px"
Responsive="true">
<Columns>
<RadzenDataGridColumn Property="@nameof(GuidModel.Id)" Title="Id" Width="100px"/>
</Columns>
</RadzenDataGrid>
</RadzenColumn>
</RadzenRow>
<EventConsole @ref="@_console" Style="width: 100%"/>
@code{
private RadzenDataGrid<GuidModel>? _dataGrid;
public IEnumerable<GuidModel>? _filteredGuids;
private int _countAll;
private IList<GuidModel>? _selectedIds;
private EventConsole _console;
private List<GuidModel>? _guidSource;
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
//CreateGuids();
}
private Task LoadData(LoadDataArgs args)
{
if (_guidSource == null) {
throw new InvalidOperationException("GuidSource is null.");
}
_filteredGuids = _guidSource.Skip(args.Skip ?? 0).Take(args.Top ?? _guidSource.Count);
_countAll = _guidSource.Count;
_console.Log($"LoadData: Count {_filteredGuids.Count()}, Skip {args.Skip}, Top {args.Top}");
return Task.CompletedTask;
}
private Task OnCreateButtonClick()
{
CreateGuids();
_dataGrid?.Reload();
_console.Log($"{_guidSource?.Count} Guid-items in list.");
return Task.CompletedTask;
}
private Task OnClearButtonClick()
{
_guidSource?.Clear();
_dataGrid?.Reload();
_console.Log($"{_guidSource?.Count} Guid-items in list.");
return Task.CompletedTask;
}
private void CreateGuids()
{
_guidSource ??= [];
for (var i = 0; i < 20; i++) {
_guidSource.Add(new() { Id = Guid.NewGuid() });
}
}