Datagrid doesn't initialize when tab change

I have Tabs with tabItem that have Datagrid each.
And i wanted to get a data when the tab change.
So i have set OnTabsChanged Parameter to get data from database.

<RadzenTabs @ref="@Tabs" @bind-SelectedIndex="selectedTab" Change="@(args => OnTabChanged(args))">

OnTabChanged calls Inquire and Inquire gets data for each grid's datasource.

private void Inquire()
{
switch (selectedTab)
{
case 0:
GridData_Inquire(ref grid1_data, 1);
break;
case 1:
GridData_Inquire(ref grid2_data, 2);
break;
case 2:
ArchiveManagement_Inquire();
break;

    }
}

And. here's a problem.
If i click second tab to see grid2, the grid 2 is not initialized so grid2.Reload() causes null reference exception.
So i put grid2.Reload() inside of AfterRendered lifecycle event to reload grid2 when it's initialized.

protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
isInitializing = false;

        GridData_Inquire(ref grid1_data, 1);
    }
    switch (selectedTab + 1)
    {
        case 1:
            grid1.Reload();
            //grid1.Count = grid1_data.Count; //Tried due to documentation said you should set count. 
            break;                                            //If i use LoadData(But i didn't used...)
        case 2:
            grid2.Reload();
            //grid2.Count = grid2_data.Count;
            break;
        case 3:

            break;

        default:
            break;
    }
}

And.... yes it doesn't worked.

So i leave a topic here.
How should i get a data, and set a data to datagrid, and reload the grid?

  • Datas are synchronously get from database with dapper and ado.net.

You can use LoadData event to set data and count to variables and bind these variables to respective properties.

Thank you for reply.

Summary for me and others.
Don't just set datasource of grid.
Let DataGrid do it.
To do.

<RadzenGrid @ref="@grid1" TItem="DataGridModel"
  Data="grid1_data" LoadData="@Inquire" Count="@grid_data.Count">

And you can get Grid's data by grid1.Reload();