C#C
C#6mo ago
Fat Lenny

Blazor Datagrid selection not working

c#

<FluentDataGrid Items="@People" ShowHover="@SelectFromEntireRow" TGridItem="Person">
    <SelectColumn TGridItem="Person"
                  SelectMode="@(DataGridSelectMode.SingleSticky)"
                  SelectFromEntireRow="@true"
                  @bind-SelectedItems="@SelectedItems" />
    <PropertyColumn Width="100px" Property="@(p => p.PersonId)" Title="ID" />
    <PropertyColumn Width="300px" Property="@(p => p.Name)" />
    <PropertyColumn Width="150px" Property="@(p => p.BirthDate)" Format="yyyy-MM-dd" Sortable="true" />
</FluentDataGrid>

<div style="margin-top: 0.5rem;">
    <b>SelectedItems:</b>
    @String.Join("; ", SelectedItems.Select(p => p.Name))
</div>

@code {
    bool UseSelectedItems = true;
    bool SelectFromEntireRow = true;
    bool SelectableBefore2000 = false;
    DataGridSelectMode Mode = DataGridSelectMode.Single;

    IEnumerable<Person> SelectedItems = People.Where(p => p.Selected);

    record Person(int PersonId, string Name, DateOnly BirthDate)
    {
        public bool Selected { get; set; }
    };

    static IQueryable<Person> People = new[]
    {
        new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)) { Selected = true },
        new Person(10944, "António Langa", new DateOnly(1991, 12, 1)),
        new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)),
        new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)),
        new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)),
        new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)),
    }.AsQueryable();

    private void ResetSelectItems()
    {
        People.ToList().ForEach(i => i.Selected = false);
        People.First().Selected = true;
        SelectedItems = People.Where(p => p.Selected);
    }
}
Was this page helpful?