Search code examples
gridblazor

FirstOrDefault working same as LastOrDefault for grid records


I have a Telerik grid that needs buttons to select the First, Last, Previous, and Next records. I have the following methods so far, but all select the last row:

private void GoToFirstRow(){
    var firstRecord = GridDataSource.FirstOrDefault();
    var collectionOfFirstRecord = GridDataSource.Where(x => x.ID == firstRecord.ID);
    SelectedItems = new List<ReportRO>(collectionOfFirstRecord);
}
private void GoToLastRow(){
    var lastRecord = GridDataSource.LastOrDefault();
    var collectionOfLastRecord = GridDataSource.Where(x => x.ID == lastRecord.ID);
    SelectedItems = new List<ReportRO>(collectionOfLastRecord);
}
private void GoToPreviousRow(){
    int prevIndex = index - 1;
    var prevRecord = GridDataSource.ElementAt(prevIndex);
    var collectionOfPrevRecord = GridDataSource.Where(x => x.ID == prevRecord.ID);
    SelectedItems = new List<ReportRO>(collectionOfPrevRecord);
}

I thought maybe the "OrDefault" part in GoToFirstRow was the issue, so I switched "FirstOrDefault" to "First," but it didn't help.

Just to be thorough, here are my grid and related elements in brief:

   <TelerikGrid   Data="@GridDataSource"
             EditMode="@Telerik.Blazor.GridEditMode.Incell"
             @ref="Grid"
             SelectionMode="Telerik.Blazor.GridSelectionMode.Single"
             @bind-SelectedItems="@SelectedItems">
@code {
[Parameter]
public ReportCollection GridDataSource { get; set; }
public TelerikGrid<ReportRO> Grid;
public IEnumerable<ReportRO> SelectedItems { get; set; } = Enumerable.Empty<ReportRO>();

Solution

  • I didn't add IDs to my dummy records. Since they were all identical ID-wise and I tied the methods to the record IDs, there was no "first" record. It works now.