Hi, This is first time I'm using Radzen Data Grid. I am simply trying to capture the selected row, when user click on a row. I have the following grid:
<RadzenDataGrid Data="@clients"
Count="@count"
LoadData="@LoadData"
AllowFiltering="true"
AllowColumnResize="true"
AllowSorting="true"
PageSize="5"
AllowPaging="true"
PagerHorizontalAlign="HorizontalAlign.Left"
ShowPagingSummary="true"
IsLoading="@isLoading"
SelectionMode="DataGridSelectionMode.Single"
@bind-Value=@selectedClients
RowClick="@OnRowClick"
TItem="Client">
<Columns>
<RadzenDataGridColumn TItem="Client" Property="Id" Title="Client ID" />
<RadzenDataGridColumn TItem="Client" Property="CompanyName" Title="Client Name" />
<RadzenDataGridColumn TItem="Client" Property="BillingAddress" Title="Address" />
<RadzenDataGridColumn TItem="Client" Property="Phone" Title="Phone" />
<RadzenDataGridColumn TItem="Client" Property="Email" Title="Email" />
</Columns>
</RadzenDataGrid>
This is my code:
@code {
IEnumerable<Client>? clients;
bool isLoading;
int count;
IList<Client>? selectedClients = new List<Client>();
void OnRowClick(DataGridRowMouseEventArgs<Client> args)
{
selectedClients = new List<Client> { args.Data };
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Info,
Summary = "Client Selected",
Detail = $"Selected {args.Data.CompanyName}",
Duration = 4000
});
StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
await LoadData(new LoadDataArgs { Skip = 0, Top = 5 });
}
async Task LoadData(LoadDataArgs args)
{
try
{
isLoading = true;
StateHasChanged();
var pageSize = args.Top ?? 5;
var skip = args.Skip ?? 0;
var pageNumber = (skip / pageSize) + 1;
var response = await Http.GetAsync($"api/clients?pageNumber={pageNumber}&pageSize={pageSize}");
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<PaginatedResult<Client>>();
if (result != null)
{
clients = result.Items.ToList();
count = result.Pagination.TotalCount;
// Only set initial selection if no row is currently selected
if (selectedClients?.Any() != true && clients.Any())
{
selectedClients = new List<Client> { clients.First() };
}
}
}
else
{
var error = await response.Content.ReadAsStringAsync();
NotificationService.Notify(NotificationSeverity.Error, "Error", $"Failed to load clients: {error}");
}
}
catch (Exception ex)
{
NotificationService.Notify(NotificationSeverity.Error, "Error", $"Failed to load clients: {ex.Message}");
}
finally
{
isLoading = false;
StateHasChanged();
}
}
When clicking on the rows, nothing happens, the row does not even select.
Thank you in advance for the help!