Completed
Last Updated: 01 Mar 2021 08:30 by ADMIN
Release 2.23.0
Wei
Created on: 11 Feb 2021 09:29
Category: Grid
Type: Bug Report
2
Changing Data from another Thread when there is Selection and the data is ObservableCollection causes System.InvalidOperationException: The current thread is not associated with the Dispatcher.

Data load in background thread with selected row on UI crashing application when there is selection

 

1. Select a row
2. Click the button
3. check the server console and/or try to do something else

@using System.Collections.ObjectModel

<TelerikGrid @ref="@Grid"
             Data="@GridData"
             SelectionMode="@GridSelectionMode.Single"
             OnRowClick="@OnRowClickHandler"
             @bind-SelectedItems="@SelectedData"
             EditMode="@GridEditMode.Incell"
             Resizable="true"
             Width="100%"
             Height="200px"
             Reorderable="true"
             Groupable="false"
             ShowColumnMenu="false"
             FilterMode="@GridFilterMode.None"
             Sortable="true">
    <GridColumns>
        <GridColumn Field=@nameof(ViewModel.Name) Editable="false">
        </GridColumn>
    </GridColumns>
</TelerikGrid>

<TelerikButton OnClick="RefreshButtonClick">Refresh</TelerikButton>

@code {
    private const int RowHeight = 40;
    private const string Height = "700px";
    private const int PageSize = 20;

    private ObservableCollection<ViewModel> GridData { get; set; } = new ObservableCollection<ViewModel>();
    private IEnumerable<ViewModel> SelectedData { get; set; } = Enumerable.Empty<ViewModel>();

    private TelerikGrid<ViewModel> Grid { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await LoadData();

        await base.OnInitializedAsync();
    }

    async Task LoadData()
    {
        var data = Enumerable.Range(1, 5).Select(x => new ViewModel { Name = $"name {x}" });
        GridData = new ObservableCollection<ViewModel>(data);
    }


    private async Task RefreshButtonClick()
    {
        Console.WriteLine("reload data");

        // spawn a new thread, a Timer will do as well
        await Task.Run(() =>
        {
            IEnumerable<ViewModel> data = new List<ViewModel> { new ViewModel { Name = "1" }, new ViewModel { Name = "2" }, new ViewModel { Name = "3" } };

            this.GridData.Clear();
            this.GridData.AddRange(data);
        });
    }

    private void OnRowClickHandler(GridRowClickEventArgs args)
    {
        Console.WriteLine("click to select");
        ViewModel viewModel = (ViewModel)args.Item;
        //this is here because of theincell editing, not relevant to the issue
        SelectedData = new List<ViewModel>() { viewModel };
    }

    public class ViewModel
    {
        public ViewModel()
        {
        }

        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
}

0 comments