Hi
I'm trying to use a Grid with a variable number of columns which can be set by a parameter. I can make the columns show up, but in some cases, the column headers remain empty. I attached a small test project which illustrates the problem. This is my component containing the Grid (MyGrid.razor in the test project):
<SfGrid DataSource="@_gridRows">
<GridColumns>
<GridColumn Field=@nameof(GridRow.Date) HeaderText="Date"></GridColumn>
@foreach (string columnHeader in _columnHeaders)
{
<GridColumn Field="@($"Values.{columnHeader}")" HeaderText="@columnHeader" />
}
</GridColumns>
</SfGrid>
@code
{
private class GridRow
{
public DateTime Date { get; }
public ExpandoObject Values { get; }
public GridRow(DateTime date, ExpandoObject values)
{
Date = date;
Values = values;
}
}
[Parameter]
public int NumberOfColumns { get; set; }
private List<GridRow> _gridRows;
private List<string> _columnHeaders;
protected override async Task OnParametersSetAsync()
{
_gridRows = new List<GridRow>();
_columnHeaders = new List<string>();
// Without this delay, it works fine
await Task.Delay(2000);
for (int i = 0; i < NumberOfColumns; i++)
{
_columnHeaders.Add($"Column_{i}");
}
for (DateTime date = DateTime.Today.AddMonths(-1); date <= DateTime.Today; date = date.AddDays(1))
{
ExpandoObject values = new ExpandoObject();
for (int i = 0; i < NumberOfColumns; i++)
{
values.TryAdd($"Column_{i}", $"{date.ToShortDateString()}_{i}");
}
_gridRows.Add(new GridRow(date, values));
}
}
}
When I run this with NumberOfColumns = 5, it looks like this:
So the columns are there, but the column headers are missing.
If I remove the "await Task.Delay(2000)", it works fine:
(in my real code, there is a database access which takes some time instead of the Task.Delay)
If I use the synchronous version OnParametersSet, it also works, even if I add a Thread.Sleep(2000). But I would prefer to use the async version.
Any idea what's wrong?
Thanks
Sven
Attachment:
BlazorApp_359ffb58.zip