Grid Selection Style Not Updating #956

Closed
opened 2026-01-29 17:46:53 +00:00 by claunia · 1 comment
Owner

Originally created by @pgodwin on GitHub (Aug 11, 2023).

Describe the bug
When using @bind-Value, changes to the bound value do not update the row style to show they are selected.

To Reproduce
Use the follwing code and press the "Select Homer" option to change the selection.

@page "/"

<RadzenButton Text="Select Homer"
    Click="@(()=>{ SelectedItems.Clear(); SelectedItems.Add(Data[0]); StateHasChanged();})"
    />

<RadzenDataGrid
    TItem="Test"
    Data="@Data"
    @ref="grid"
    SelectionMode="DataGridSelectionMode.Multiple"
    AllowRowSelectOnRowClick="false"
    @bind-Value="SelectedItems">
    <Columns>
        <RadzenDataGridColumn TItem="Test"
                              Width="60px"
                              Sortable="false"
                              Filterable="false">
            <HeaderTemplate>
                <RadzenCheckBox TValue="bool"
                                TriState="false"
                                Value="@(this.Data?.Any(i => SelectedItems != null && SelectedItems.Contains(i)) ?? false)"
                                Change="@(selected => SelectedItems = selected ? Data?.ToList() : new List<Test>())" />

            </HeaderTemplate>
            <Template Context="row">
                <RadzenCheckBox TValue="bool"
                                TriState="false"
                                Value="@(SelectedItems != null && SelectedItems.Contains(row))"
                                Change="@(selected => { if (selected) SelectedItems?.Add(row); else SelectedItems?.Remove(row); })" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Test" Title="Id" Property="Id" />
        <RadzenDataGridColumn TItem="Test" Title="Name" Property="Name" />
    </Columns>
</RadzenDataGrid>

@code {
    
    public class Test
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public RadzenDataGrid<Test> grid;

    public IList<Test> SelectedItems = new List<Test>();

    public List<Test> Data = new()
    {
        new Test { Id = 1, Name = "Homer" },
        new Test { Id = 2, Name = "Bart" }
    };
}

Expected behavior
The items in the bound value should be displayed with the selection style on the row.

Screenshots
Behaviour after pressing "SELECT HOMER"
image

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser Chrome
  • Version: 115.0.5790.111
  • Radzen: 4.15.3
Originally created by @pgodwin on GitHub (Aug 11, 2023). <!-- IMPORTANT: Read this first!!! 1. If you own a Radzen Professional or Еnterprise subscription you can report your issue or ask us a question via email at info@radzen.com. Radzen staff will reply within 24 hours (Professional) or 16 hours (Enterprise) 2. The Radzen staff guarantees a response to issues in this repo only to paid subscribers. 3. If you have a HOW TO question start a new forum thread in the Radzen Community forum: https://forum.radzen.com. Radzen staff will close issues that are HOWTO questions. 4. Please adhere to the issue template. Specify all the steps required to reproduce the issue or link a project which reproduces it easily (without requiring extra steps such as restoring a database). --> **Describe the bug** When using `@bind-Value`, changes to the bound value do not update the row style to show they are selected. **To Reproduce** Use the follwing code and press the "Select Homer" option to change the selection. ```csharp @page "/" <RadzenButton Text="Select Homer" Click="@(()=>{ SelectedItems.Clear(); SelectedItems.Add(Data[0]); StateHasChanged();})" /> <RadzenDataGrid TItem="Test" Data="@Data" @ref="grid" SelectionMode="DataGridSelectionMode.Multiple" AllowRowSelectOnRowClick="false" @bind-Value="SelectedItems"> <Columns> <RadzenDataGridColumn TItem="Test" Width="60px" Sortable="false" Filterable="false"> <HeaderTemplate> <RadzenCheckBox TValue="bool" TriState="false" Value="@(this.Data?.Any(i => SelectedItems != null && SelectedItems.Contains(i)) ?? false)" Change="@(selected => SelectedItems = selected ? Data?.ToList() : new List<Test>())" /> </HeaderTemplate> <Template Context="row"> <RadzenCheckBox TValue="bool" TriState="false" Value="@(SelectedItems != null && SelectedItems.Contains(row))" Change="@(selected => { if (selected) SelectedItems?.Add(row); else SelectedItems?.Remove(row); })" /> </Template> </RadzenDataGridColumn> <RadzenDataGridColumn TItem="Test" Title="Id" Property="Id" /> <RadzenDataGridColumn TItem="Test" Title="Name" Property="Name" /> </Columns> </RadzenDataGrid> @code { public class Test { public int Id { get; set; } public string Name { get; set; } } public RadzenDataGrid<Test> grid; public IList<Test> SelectedItems = new List<Test>(); public List<Test> Data = new() { new Test { Id = 1, Name = "Homer" }, new Test { Id = 2, Name = "Bart" } }; } ``` **Expected behavior** The items in the bound value should be displayed with the selection style on the row. **Screenshots** Behaviour after pressing "SELECT HOMER" <img width="1100" alt="image" src="https://github.com/radzenhq/radzen-blazor/assets/1046558/ce884a4a-e305-483b-8267-73f454b69428"> **Desktop (please complete the following information):** - OS: Windows 11 - Browser Chrome - Version: 115.0.5790.111 - Radzen: 4.15.3
Author
Owner

@enchev commented on GitHub (Aug 11, 2023):

This code will not invoke Parameter change of the Blazor component:

Click="@(()=>{ SelectedItems.Clear(); SelectedItems.Add(Data[0]); StateHasChanged();})"

This code will:

Click="@(()=> SelectedItems = Data.Where(i => i.Id == 1).ToList())" />

Blazor components will not listed to any collection change events .. not to mention that IList does not have such.

@enchev commented on GitHub (Aug 11, 2023): This code will not invoke Parameter change of the Blazor component: ``` Click="@(()=>{ SelectedItems.Clear(); SelectedItems.Add(Data[0]); StateHasChanged();})" ``` This code will: ``` Click="@(()=> SelectedItems = Data.Where(i => i.Id == 1).ToList())" /> ``` Blazor components will not listed to any collection change events .. not to mention that IList does not have such.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#956