Radzen Grid : Creating and filling columns dynamically

Hello,

I am trying to add columns dynamically to a Radzen Grid. I put my code below.
So, I have the index j to loop through all the columns. It's is working well and it is generating the columns with different names according to my List SubArticles data.
But, my problem is that in my index j is undefined, so I cannot fill each cell with the correct data according to its column. I would like to have this.

   <Template Context="data">                                                           
          @(data.Attributes[j].Value)
   </Template>

Is there a way I can access to my column index from Template Context="data" ?

<RadzenGrid @ref="SubArticlesTable" Count="@TotalItems" Data="@SubArticles" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true"
                FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.Simple"
                AllowPaging="true" TItem="MyViewModel">
    <Columns>                                                                                                        
            <RadzenGridColumn TItem="MyViewModel" Property="Code" Sortable="false" Title="Code" Filterable="false" Width="100px">
            </RadzenGridColumn>                                                                                                    
            @for (int j = 0; j < this.NumberOfDifferentiatingAttributes; j++)
	        {
                <RadzenGridColumn TItem="MyViewModel" Title="@(SubArticles[0].Attributes.Label)" Sortable="false" Filterable="false" Width="200px">
                    <Template Context="data">                                                           
                       @(data.Attributes[0].Value)
                   </Template>
                </RadzenGridColumn>
		    }
    </Columns>
</RadzenGrid>

Could it be a capture issue? Try creating a copy of the loop index:

@for (int j = 0; j < this.NumberOfDifferentiatingAttributes; j++)
{
    @{ int columnIndex = j; }
   <RadzenGridColumn TItem="MyViewModel" Title="@(SubArticles[0].Attributes.Label)" Sortable="false" Filterable="false" Width="200px">
        <Template Context="data">                                                           
          @(data.Attributes[columnIndex].Value)
       </Template>
   </RadzenGridColumn>
}
1 Like

I have just replaced

@{ int columnIndex = j; }

by

int columnIndex = j;

in the code you provided.

It is working like a charm now. Thank you so much !