How to add GridColumn definitions to SfGrid[T] using RenderTreeBuilder

Hello,

I am using RenderTreeBuilder to programmatically generate some Blazor components. When rendering a SfGrid<T>, I can bind the DataSource successfully and see rows appear — but only if I don’t define any columns manually.

When I add GridColumn components via ChildContent, there is an infinite loop generated; if I add the columns as a parameter "Columns", the columns show up in the header, but no data appears in the grid.

Example structure:

builder.OpenComponent(typeof(SfGrid<>).MakeGenericType(tItem), seq++);
builder.AddAttribute(seq++, "DataSource", myData);

builder.AddAttribute(seq++, "ChildContent", (RenderFragment)(gridBuilder =>
{
    gridBuilder.OpenComponent(typeof(GridColumns), seq++);
    foreach (var col in myColumnDefs)
    {
        gridBuilder.OpenComponent(typeof(GridColumn), seq++);
        gridBuilder.AddAttribute(seq++, "Field", col.Field); // e.g., "Name"
        gridBuilder.AddAttribute(seq++, "HeaderText", col.HeaderText);
        gridBuilder.AddAttribute(seq++, "AutoFit", true);
        gridBuilder.CloseComponent();
    }
    gridBuilder.CloseComponent(); // GridColumns
}));
builder.CloseComponent(); // SfGrid

Questions:

  • Is this the correct way to define GridColumn components via RenderTreeBuilder?

  • Are there any required attributes or steps to ensure proper data binding?


Thanks in advance,
Cristobal.


4 Replies 1 reply marked as answer

JU Jawad ul Hassan May 13, 2025 06:12 AM UTC

You're close! The issue is likely caused by wrapping GridColumn components inside a GridColumns component — that's not needed and can cause problems. Instead, add GridColumn components directly inside the ChildContent:

csharp
builder.OpenComponent(seq++, typeof(SfGrid<>).MakeGenericType(tItem)); builder.AddAttribute(seq++, "DataSource", myData); builder.AddAttribute(seq++, "ChildContent", (RenderFragment)(gridBuilder => { foreach (var col in myColumnDefs) { gridBuilder.OpenComponent(seq++, typeof(GridColumn)); gridBuilder.AddAttribute(seq++, "Field", col.Field); gridBuilder.AddAttribute(seq++, "HeaderText", col.HeaderText); gridBuilder.AddAttribute(seq++, "AutoFit", true); gridBuilder.CloseComponent(); } })); builder.CloseComponent();

Make sure col.Field matches your data model property names exactly. That should fix the issue!



PS Prathap Senthil Syncfusion Team May 13, 2025 08:38 AM UTC

Hi Cristobal,


Based on your query, we have prepared a sample using RenderTreeBuilder to create the Grid component. Please refer to the attached sample to create the Grid component on your side. Thank you.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DynamicGrid


Regards,
Prathap Senthil


Marked as answer

CR Cristobal May 13, 2025 08:16 PM UTC

Thank you both.

I now have a basis to work from. If I encounter any other issues, I'll make sure to let you know.

Regards,

Cristobal.




PS Prathap Senthil Syncfusion Team May 14, 2025 06:17 AM UTC

Thanks for the update. We are happy to hear that the provided information was helpful.


Please feel free to get back to us if you have any further queries.


Loader.
Up arrow icon