RadzenGrid Reload in async context

Hi everyone,

i am using the RadzenGrid as followed

<RadzenGrid @ref="userGrid" TItem="Account" Data="listOfUsers" Value="account" RowSelect="@(arg => account = arg)">
    <Columns>
    <RadzenGridColumn TItem="Account" Property="Id" Title="Id" />
    <RadzenGridColumn TItem="Account" Property="UserName" Title="Benutzername" />
    </Columns>
    </RadzenGrid>

My DatabaseContext i inject by IdbContextFactory like that:

@inject IDbContextFactory<DatabaseContext> DbFactory

@code {
protected override async Task OnInitializedAsync()
    {
        using (var dbContext = DbFactory.CreateDbContext())
        {
            account = await dbContext.Users.Include(x => x.UserRoles).ThenInclude(y => y.Role).FirstOrDefaultAsync();
            listOfUsers = await dbContext.Users.ToListAsync();
        }
        await base.OnInitializedAsync();
    }
}

If something in my "listOfUsers" Collection then i want to "reload" it, but because i can't use the "Users"-DbSet its not reloading.
Do you guys have any idea to solve that?

Enjoy your help! Thanks!

If you want to reload the listOfUsers collection from your database you have to read it again from the dbContext. There is no way around that.

Thanks @korchev for your replay. The Problem i've with that solution is, that when i'm reassign the listOfUsers the selected account of the DataGrid gets unselect. Do i have to clear the existing list instead of reassign it?

Hi @JoeGER94,

Try persisting the selection of your RadzenGrid by using @bind-Value.

<RadzenGrid @bind-Value=@selection ...>

@code {
     object selection;
}

We recommend updating your application to use the newer RadzenDataGrid though.

Hey @korchev,

i did a lot of testing now and i updated to the current Version as you recommended.

<RadzenDataGrid @ref="usersGrid" TItem="Account" Data="listOfAccounts" @bind-Value="selectedAccountsList" RowSelect="@(args => OnRowSelected(args))" SelectionMode="DataGridSelectionMode.Single">
<Columns>
   <RadzenDataGridColumn TItem="Account" Property="Id" Title="Id" />
   <RadzenDataGridColumn TItem="Account" Property="UserName" Title="Benutzername" />
</Columns>
</RadzenDataGrid>

As soon as i reassign the listOfAccounts with

listOfAccounts = await context.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ToListAsync();

the RadzenDataGrid still lose his selectedValue though, however the selectedAccountsList is not empty. To solve this i clear the selectedAccountsList and fill it again. But to do that i need to "cache" the currentSelectedAccounts. It's working but it's not really nice...

I wrote some code if you want to check it:

@if (listOfUsers != null)
{
    <RadzenDataGrid TItem="Account" Data="listOfUsers" @bind-Value="selectedUsers" SelectionMode="DataGridSelectionMode.Single" RowSelect="@(args => RowSelected(args))" RowDeselect="@(args => RowDeselected(args))">
        <Columns>
            <RadzenDataGridColumn TItem="Account" Property="Username" />
        </Columns>
    </RadzenDataGrid>
    <br />
    <RadzenLabel Text="@selectedUsers.FirstOrDefault().Username" />
}

<RadzenButton Text="SomethingChanged" Click=@SomethingChanged />

@code {

    IList<Account> listOfUsers;
    IList<Account> selectedUsers;

    public class Account
    {
        public int Id { get; set; }
        public string Username { get; set; }
    }

    protected override async Task OnInitializedAsync()
    {
        await GenerateSomeTestUsers();
        
        await base.OnInitializedAsync();
    }

    async Task GenerateSomeTestUsers()
    {
        await Task.Run(() =>
        {
            listOfUsers = new List<Account>();
            for (int i = 0; i < 11; i++)
            {
                Account acc = new Account();
                acc.Id = new Random().Next();
                acc.Username = "UserName " + acc.Id;
                listOfUsers.Add(acc);
            }
            selectedUsers = new List<Account>();
            selectedUsers.Add(listOfUsers.FirstOrDefault());
        });
    }

    void RowSelected(Account selected)
    {
        selectedUsers.Clear();
        selectedUsers.Add(selected);
    }

    async Task SomethingChanged()
    {
        await Task.Run(() =>
        {
            listOfUsers = new List<Account>();
            for (int i = 0; i < 11; i++)
            {
                Account acc = new Account();
                acc.Id = new Random().Next();
                acc.Username = "UserName " + acc.Id;
                listOfUsers.Add(acc);
            }
        });
    }
}