Selected Items reset on RadzenDropDownDataGrid #440

Closed
opened 2026-01-29 17:37:19 +00:00 by claunia · 2 comments
Owner

Originally created by @matjankow on GitHub (Jun 20, 2022).

Describe the bug
Using RadzenDropDownDataGrid with Multiple="true" resets the selection on certain occasions, e.g. when State changes occur together with background operations. I suspect the problem is that the underlying RadzenDataGrid does have set SelectionMode=Single (instead of Multiple if RadzenDropDownDataGrid Mutliple=true) and acts "wrong" in OnRowSelect

internal async System.Threading.Tasks.Task OnRowSelect(object item, bool raiseChange = true)
{
	// ... snipped some stuff

	_value = SelectionMode == DataGridSelectionMode.Multiple ? new List<TItem>(value) : new List<TItem>() { value.FirstOrDefault() };

        // always send only one item because SelectionMode is always= Single
	await ValueChanged.InvokeAsync(_value);
	StateHasChanged();
}

To Reproduce
Also see attached sample project:

  1. Open Dropdown
  2. Select first item
  3. Select second item
  4. Select third item -> Other items are unselected

Only happens if some actions occur in the "Change" EventCallback that require state changes/background ops.

Expected behavior
All items stay selected.

Additional context
Using Radzen.Blazor Version 3.18.16

RadzenDropDownDataGrid_SelectionError.zip

Originally created by @matjankow on GitHub (Jun 20, 2022). **Describe the bug** Using RadzenDropDownDataGrid with Multiple="true" resets the selection on certain occasions, e.g. when State changes occur together with background operations. I suspect the problem is that the underlying RadzenDataGrid does have set SelectionMode=Single (instead of Multiple if RadzenDropDownDataGrid Mutliple=true) and acts "wrong" in OnRowSelect ``` internal async System.Threading.Tasks.Task OnRowSelect(object item, bool raiseChange = true) { // ... snipped some stuff _value = SelectionMode == DataGridSelectionMode.Multiple ? new List<TItem>(value) : new List<TItem>() { value.FirstOrDefault() }; // always send only one item because SelectionMode is always= Single await ValueChanged.InvokeAsync(_value); StateHasChanged(); } ``` **To Reproduce** Also see attached sample project: 1. Open Dropdown 2. Select first item 3. Select second item 4. Select third item -> Other items are unselected Only happens if some actions occur in the "Change" EventCallback that require state changes/background ops. **Expected behavior** All items stay selected. **Additional context** Using Radzen.Blazor Version 3.18.16 [RadzenDropDownDataGrid_SelectionError.zip](https://github.com/radzenhq/radzen-blazor/files/8939780/RadzenDropDownDataGrid_SelectionError.zip)
Author
Owner

@enchev commented on GitHub (Jun 22, 2022):

Here is an example based on our demo that works properly:

@page "/"
@using System.Collections

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<div class="row">
    <div class="col-6">
        <RadzenDropDownDataGrid @ref=grid AllowClear="true" Multiple="true"
                                @bind-Value=@SelectedItems 
                                Data=DataList AllowRowSelectOnRowClick="@allowRowSelectOnRowClick"
                                TextProperty="Text" ValueProperty="Id" Change=@(OnChangeSelection)>
            <Columns>
                <RadzenDropDownDataGridColumn Width="40px" Sortable="false">
                            <HeaderTemplate>
                                <RadzenCheckBox TriState="false" TValue="bool" Value="@(DataList.Any(c => SelectedItems != null && SelectedItems.Contains(c.Id)))"
                                                Change="@(args => SelectedItems = args ? grid.View.Cast<DataItem>().Select(c => c.Id) : SelectedItems = Enumerable.Empty<int>())"/>
                            </HeaderTemplate>
                            <Template Context="data">
                                <RadzenCheckBox TriState="false" Value="@(SelectedItems != null && SelectedItems.Contains(((DataItem)data).Id))" 
                                                TValue="bool" Change=@(args => { if(!allowRowSelectOnRowClick) { grid.SelectItem(data); }})/>
                            </Template>
                        </RadzenDropDownDataGridColumn>
                <RadzenDropDownDataGridColumn Property="Text" Title="Text" />
            </Columns>
        </RadzenDropDownDataGrid>
    </div>

    <div class="col-6">
        <RadzenLabel Text="@DataText"></RadzenLabel>
    </div>
    
    
</div>

@code {
    bool allowRowSelectOnRowClick;
    RadzenDropDownDataGrid<IEnumerable<int>> grid;

    public class DataItem
    {
        public int Id { get; set; }
        public string Text { get; set; }
    }

    private List<DataItem> DataList { get; set; }
    
    protected override Task OnInitializedAsync()
    {
        DataList = new List<DataItem>();
        for (int i = 1; i <= 10; i++)
        {
            DataList.Add(new DataItem
                {
                    Id = i,
                    Text = $"Item {i}"
                });
        }

        return base.OnInitializedAsync();
    }

    string DataText { get; set; }

    IEnumerable<int> SelectedItems { get; set; }

    private async Task OnChangeSelection()
    {
        // needs to be an async operation
        await Task.Run(() =>
        {
            DataText = $"Selected Items Count: {SelectedItems.Count()}";
        });
    }
}
@enchev commented on GitHub (Jun 22, 2022): Here is an example based on [our demo](https://blazor.radzen.com/dropdown-datagrid) that works properly: ``` @page "/" @using System.Collections <PageTitle>Index</PageTitle> <h1>Hello, world!</h1> <div class="row"> <div class="col-6"> <RadzenDropDownDataGrid @ref=grid AllowClear="true" Multiple="true" @bind-Value=@SelectedItems Data=DataList AllowRowSelectOnRowClick="@allowRowSelectOnRowClick" TextProperty="Text" ValueProperty="Id" Change=@(OnChangeSelection)> <Columns> <RadzenDropDownDataGridColumn Width="40px" Sortable="false"> <HeaderTemplate> <RadzenCheckBox TriState="false" TValue="bool" Value="@(DataList.Any(c => SelectedItems != null && SelectedItems.Contains(c.Id)))" Change="@(args => SelectedItems = args ? grid.View.Cast<DataItem>().Select(c => c.Id) : SelectedItems = Enumerable.Empty<int>())"/> </HeaderTemplate> <Template Context="data"> <RadzenCheckBox TriState="false" Value="@(SelectedItems != null && SelectedItems.Contains(((DataItem)data).Id))" TValue="bool" Change=@(args => { if(!allowRowSelectOnRowClick) { grid.SelectItem(data); }})/> </Template> </RadzenDropDownDataGridColumn> <RadzenDropDownDataGridColumn Property="Text" Title="Text" /> </Columns> </RadzenDropDownDataGrid> </div> <div class="col-6"> <RadzenLabel Text="@DataText"></RadzenLabel> </div> </div> @code { bool allowRowSelectOnRowClick; RadzenDropDownDataGrid<IEnumerable<int>> grid; public class DataItem { public int Id { get; set; } public string Text { get; set; } } private List<DataItem> DataList { get; set; } protected override Task OnInitializedAsync() { DataList = new List<DataItem>(); for (int i = 1; i <= 10; i++) { DataList.Add(new DataItem { Id = i, Text = $"Item {i}" }); } return base.OnInitializedAsync(); } string DataText { get; set; } IEnumerable<int> SelectedItems { get; set; } private async Task OnChangeSelection() { // needs to be an async operation await Task.Run(() => { DataText = $"Selected Items Count: {SelectedItems.Count()}"; }); } } ```
Author
Owner

@matjankow commented on GitHub (Jun 22, 2022):

This works, but is more a Workaround instead of a bug fix for me. Using AllowRowSelectOnRowClick=false seems to fix it somehow, but now i can't click on the row anymore for selecting. As soon as AllowRowSelectOnRowClick is set to true, the error/strange behavior occurs again.

@matjankow commented on GitHub (Jun 22, 2022): This works, but is more a Workaround instead of a bug fix for me. Using AllowRowSelectOnRowClick=false seems to fix it somehow, but now i can't click on the row anymore for selecting. As soon as AllowRowSelectOnRowClick is set to true, the error/strange behavior occurs again.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#440