radzenhq / radzen-blazor

Radzen Blazor is a set of 90+ free native Blazor UI components packed with DataGrid, Scheduler, Charts and robust theming including Material design and FluentUI.
https://www.radzen.com
MIT License
3.52k stars 786 forks source link

DataGrid selection not work with IQueryable #1724

Closed franklupo closed 1 week ago

franklupo commented 1 week ago

With data IQueryable the selection disappears when scroll. For testing copy source in https://blazor.radzen.com/datagrid-virtualization?theme=fluent and run

@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inherits DbContextPage

<RadzenDataGrid Data="@orderDetails" AllowVirtualization="true" Style="height:400px"
                AllowFiltering="true" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" LogicalFilterOperator="LogicalFilterOperator.Or"
                AllowSorting="true"
                @bind-Value=@selectedEmployees>
    <Columns>
        <RadzenDataGridColumn Property="@nameof(OrderDetail.OrderID)" Title="OrderID" />
        <RadzenDataGridColumn Property="@nameof(OrderDetail.ProductID)" Title="ProductID" />
        <RadzenDataGridColumn Property="@nameof(OrderDetail.UnitPrice)" Title="Unit Price">
            <Template Context="detail">
                @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(OrderDetail.Quantity)" Title="Quantity" />
        <RadzenDataGridColumn Property="@nameof(OrderDetail.Discount)" Title="Discount">
            <Template Context="detail">
                @String.Format("{0}%", detail.Discount * 100)
            </Template>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

@code {
    IQueryable<OrderDetail> orderDetails;
    IList<OrderDetail> selectedEmployees;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

  orderDetails = Enumerable.Range(1, 100).Select(a => new OrderDetail
  {
      OrderID = a,
      ProductID = a
  }).AsQueryable();

//        orderDetails = dbContext.OrderDetails;
    }
}
enchev commented 1 week ago

dbContext.OrderDetails is also IQueryable however the selection will work normally. In your case you return different data item with different HashCode every time when collection is accessed - no selection can be preserved with such binding.

franklupo commented 1 week ago

Ok, is it possible to specify a key property to keep the selection?