No rows added to empty DataGrid

When I click the 'Add taxes' button no rows are added to the grid. When however there is a least one row in the grid a new row is added.

@page "/taxes"
@inherits PageBase
@using SessyData.Model
@using Radzen

<a>Taxes</a>

<RadzenDataGrid @ref="taxesGrid"
                Data="@TaxesList"
                Context="data"
                Count="@count"
                LoadData="@LoadData"
                AllowSorting="true"
                AllowFiltering="true"
                AllowPaging="true"
                PageSize="20"
                Density="Density.Compact"
                PagerHorizontalAlign="HorizontalAlign.Center">
    <HeaderTemplate>
        <RadzenButton ButtonStyle="ButtonStyle.Success" Icon="add_circle" Text="Add taxes" Click="@InsertRow" />
    </HeaderTemplate>
    <Columns>
        <RadzenDataGridColumn Property="@nameof(Taxes.Time)" Title="Time" SortOrder="SortOrder.Ascending">
            <EditTemplate Context="fr">
                <RadzenDatePicker @bind-Value="fr.Time" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Taxes.EnergyTax)" Title="Energy tax">
            <EditTemplate Context="fr">
                <RadzenNumeric @bind-Value="fr.EnergyTax" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Taxes.ValueAddedTax)" Title="Value added tax">
            <EditTemplate Context="fr">
                <RadzenNumeric @bind-Value="fr.ValueAddedTax" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Context="order" Filterable="false" Sortable="false" TextAlign="TextAlign.Right" Frozen="true" FrozenPosition="FrozenColumnPosition.Right">
            <Template Context="fr">
                <RadzenButton Icon="add_circle"
                              ButtonStyle="ButtonStyle.Success"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              Click="@(() => InsertAfterRow(fr))"
                              title="Add new row after this row" />
                <RadzenButton Icon="edit"
                              ButtonStyle="ButtonStyle.Light"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => EditRow(fr))"
                              @onclick:stopPropagation="true" />
                <RadzenButton Icon="delete"
                              ButtonStyle="ButtonStyle.Danger"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => DeleteRow(fr))"
                              @onclick:stopPropagation="true" />
            </Template>
            <EditTemplate Context="fr">
                <RadzenButton Icon="check"
                              ButtonStyle="ButtonStyle.Success"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Click="@((args) => SaveRow(fr))" aria-label="Save" />
                <RadzenButton Icon="close"
                              ButtonStyle="ButtonStyle.Light"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              class="rz-my-1 rz-ms-1"
                              Click="@((args) => CancelEdit(fr))" aria-label="Cancel" />
                <RadzenButton Icon="delete"
                              ButtonStyle="ButtonStyle.Danger"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => DeleteRow(fr))" aria-label="Delete" />
            </EditTemplate>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>
        async Task InsertRow()
        {
            if (!taxesGrid!.IsValid) return;

            var taxes = new Taxes();
            await taxesGrid.InsertRow(taxes);
        }

We cannot run this code and cannot comment why InsertRow() might not work. I can only speculate that TaxesList is null.


@page "/taxes"
@using Radzen

<a>Taxes</a>

<RadzenDataGrid @ref="taxesGrid"
                Data="@TaxesList"
                TItem="Taxes"
                Context="data"
                Count="@count"
                LoadData="@LoadData"
                RowUpdate="@OnUpdateRow"
                RowCreate="@OnCreateRow"
                AllowSorting="true"
                AllowFiltering="true"
                AllowPaging="true"
                PageSize="20"
                Density="Density.Compact"
                PagerHorizontalAlign="HorizontalAlign.Center">
    <HeaderTemplate>
        <RadzenButton ButtonStyle="ButtonStyle.Success" Icon="add_circle" Text="Add taxes" Click="@InsertRow" />
    </HeaderTemplate>
    <Columns>
        <RadzenDataGridColumn Property="@nameof(Taxes.Time)" Title="Time" SortOrder="SortOrder.Ascending">
            <EditTemplate Context="fr">
                <RadzenDatePicker @bind-Value="fr.Time" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Taxes.EnergyTax)" Title="Energy tax">
            <EditTemplate Context="fr">
                <RadzenNumeric @bind-Value="fr.EnergyTax" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Taxes.ValueAddedTax)" Title="Value added tax">
            <EditTemplate Context="fr">
                <RadzenNumeric @bind-Value="fr.ValueAddedTax" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Taxes.TaxReduction)" Title="Tax reduction">
            <EditTemplate Context="fr">
                <RadzenNumeric @bind-Value="fr.TaxReduction" Style="width:100%" />
            </EditTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Context="order" Filterable="false" Sortable="false" TextAlign="TextAlign.Right" Frozen="true" FrozenPosition="FrozenColumnPosition.Right">
            <Template Context="fr">
                <RadzenButton Icon="add_circle"
                              ButtonStyle="ButtonStyle.Success"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              Click="@(() => InsertAfterRow(fr))"
                              title="Add new row after this row" />
                <RadzenButton Icon="edit"
                              ButtonStyle="ButtonStyle.Light"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => EditRow(fr))"
                              @onclick:stopPropagation="true" />
                <RadzenButton Icon="delete"
                              ButtonStyle="ButtonStyle.Danger"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => DeleteRow(fr))"
                              @onclick:stopPropagation="true" />
            </Template>
            <EditTemplate Context="fr">
                <RadzenButton Icon="check"
                              ButtonStyle="ButtonStyle.Success"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Click="@((args) => SaveRow(fr))" aria-label="Save" />
                <RadzenButton Icon="close"
                              ButtonStyle="ButtonStyle.Light"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              class="rz-my-1 rz-ms-1"
                              Click="@((args) => CancelEdit(fr))" aria-label="Cancel" />
                <RadzenButton Icon="delete"
                              ButtonStyle="ButtonStyle.Danger"
                              Variant="Variant.Flat"
                              Size="ButtonSize.Small"
                              Shade="Shade.Lighter"
                              class="rz-my-1 rz-ms-1"
                              Click="@(args => DeleteRow(fr))" aria-label="Delete" />
            </EditTemplate>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

@code {
    int count { get; set; }
	
    List<Taxes>? TaxesList { get; set; } = new();

    RadzenDataGrid<Taxes>? taxesGrid { get; set; }

    void LoadData(LoadDataArgs args)
    {
        Console.WriteLine ("LoadData firing");
        
        if(TaxesList.Count == 0)
        {
            AddRowToTaxesList();   
        }
    }

    void AddRowToTaxesList()
    {
        TaxesList.Add( 
            new Taxes
            {
                Id = 1,
                Time = DateTime.Now,
                EnergyTax = 0.12345,
                ValueAddedTax = 1.2345
            });
    
        count = TaxesList.Count;
    }
	
	async Task EditRow(Taxes taxes)
	{
		if (!taxesGrid!.IsValid) return;

		await taxesGrid!.EditRow(taxes);
	}

	void OnUpdateRow(Taxes taxes)
	{
	}

	async Task SaveRow(Taxes taxes)
	{
		await taxesGrid!.UpdateRow(taxes);
	}

	async Task CancelEdit(Taxes taxes)
	{
		taxesGrid!.CancelEditRow(taxes);

		await taxesGrid.Reload();
	}

	async Task DeleteRow(Taxes taxes)
	{
		await taxesGrid!.Reload();
	}

	async Task InsertRow()
	{
		if (!taxesGrid!.IsValid) return;

		var taxes = new Taxes { Time = DateTime.Now.Date };
		await taxesGrid.InsertRow(taxes);
	}

	async Task InsertAfterRow(Taxes row)
	{
		if (!taxesGrid!.IsValid) return;

		var taxes = new Taxes();
		await taxesGrid.InsertAfterRow(taxes, row);
	}

	void OnCreateRow(Taxes taxes)
	{
	}
	
	public class Taxes
	{
		public int Id { get; set; }
		public DateTime? Time { get; set; }
		public double EnergyTax { get; set; }
		public double ValueAddedTax { get; set; }
		public double TaxReduction { get; set; }

		public override string ToString()
		{
			return $"Time: {Time}, Energy tax {EnergyTax}, Value added tax {ValueAddedTax}";
		}
	}
}

Are you refering to that post because of the 'count' that has to be set?

The problem was in the LoadData not being called at first render. The grid.FirstPage() was wrong. That had to be grid.FirstPage(true). Now it's working.