Debugging Recommendations for DataGrid

I have a datagrid that loads up and runs but does not load data even though I have established breakpoints and can show the data is there. So it seems like the data grid is just oblivious to the fact that there is data for it to show and nothing that I try gets it working. I 100% assume this is something stupid but after several hours I figured I might as well ask the community.

<RadzenDataGrid @ref="booksGrid" Data="@books" TItem="NewBook"
                AllowFiltering="true" AllowPaging="true" PageSize="@pageSize" AllowSorting="true"
                LoadData="@OnLoadData" RowUpdate="@OnRowUpdate"
                ColumnWidth="200px">
    <Columns>
        <RadzenDataGridColumn TItem="NewBook" Property="Title" Title="Title">
            <EditTemplate Context="data">
                <RadzenTextBox @bind-Value="data.Title" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="NewBook" Property="ASIN" Title="ASIN">
            <EditTemplate Context="data">
                <RadzenTextBox @bind-Value="data.ASIN" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="NewBook" Property="Description" Title="Description">
            <EditTemplate Context="data">
                <RadzenTextBox @bind-Value="data.Description" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="NewBook" Property="Twitter" Title="Twitter">
            <EditTemplate Context="data">
                <RadzenTextBox @bind-Value="data.Twitter" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="NewBook" Property="IsContacted" Title="Is Contacted?">
            <EditTemplate Context="data">
                <RadzenCheckBox @bind-Value="data.IsContacted" />
            </EditTemplate>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

Here is my markup and here is the code behind (I have tried this no less than 100 ways so this is just one of many attempts):

    public partial class NewBookComm : ComponentBase
    {
        [Inject] private IServiceScopeFactory ScopeFactory { get; set; }
        [Inject] private NavigationManager NavigationManager { get; set; }

        private RadzenDataGrid<NewBook> booksGrid;
        private IEnumerable<NewBook> books;
        private int totalBooks;
        private int currentPage = 1;
        private int pageSize = 10;

        protected override async Task OnInitializedAsync()
        {
            await LoadDataOnInit();
        }

        private async Task LoadDataOnInit()
        {
            books = new List<NewBook>();
            LoadDataArgs args = new LoadDataArgs
            {
                Skip = 0,
                Top = pageSize,
                OrderBy = null,
                Filter = null
            };

            await OnLoadData(args);
        }

        private async Task OnLoadData(LoadDataArgs args)
        {
            using (var serviceScope = ScopeFactory.CreateScope())
            {
                var aService = serviceScope.ServiceProvider.GetService<OpenAIService>();

                // Handle potential null values in args
                int skip = args.Skip ?? 0;  // Default to 0 if null
                int top = args.Top ?? pageSize;  // Use existing pageSize if null

                int page = (skip / top) + 1;
                pageSize = top;
                string filter = args.Filter;
                string orderBy = args.OrderBy;

                // Fetch data
                var result = await aService.GetBooksAsync(page, pageSize, filter, orderBy);

                books = new List<NewBook>();
                foreach(var b in result.Items)
                {
                    books.Append(b);
                }
                totalBooks = result.TotalCount;

                StateHasChanged();
            }
        }

        private async Task OnRowUpdate(NewBook book)
        {
            using (var serviceScope = ScopeFactory.CreateScope())
            {
                var aService = serviceScope.ServiceProvider.GetService<OpenAIService>();
                await aService.UpdateBookAsync(book);
                StateHasChanged();
            }
        }```
books has data but it just never loads into the grid even though it is clearly set. What am I missing here? Thanks for taking a look! I didn't provide the service code here but it is working and giving me back data so that is not my problem. The grid is just not loading anything no matter what I try.

You have missed to set Count - this is mandatory for LoadData binding.

1 Like