Reordering columns programmatically

I was trying to be clever and reuse the same grid for multiple views by changing the column property values using @ref. The property value changes ( I can see the new value in the columnsCollection ) but the grid seems unaware and displays what it was initially configured with.

Is there some way to programmatically change the order of columns on an existing datagrid? or someway to make the grid realize that its column properties have changed?


As far as I understand you want to reuse the same DataGrid with different data - in this case maybe the best way will be to create all columns in a loop from current data properties.

Thanks for the reply.

It is the same data really with some of the columns hidden, different header titles etc. By changing the values in the arrays below I can hide certain columns, adjust the width and change the title. But the Property=@propertyBase part doesn't seem to work. The Property changes but the grid doesn't. So for example if the first column had a Property of Fruit and listed assorted fruit and then I changed the first propertyBase element to be Vegetables it would still show the fruit data even after a reload of the grid or reloading the data.

<RadzenDataGridColumn TItem="ResultObj" Visible=@IsVisibleBase[0] Property=@propertyBase[0] Title=@columnHeaderBase[0] Width="@columnWidthBase[0]" />
                    <RadzenDataGridColumn TItem="ResultObj" Visible=@IsVisibleBase[1] Property=@propertyBase[1] Title=@columnHeaderBase[1] Width="@columnWidthBase[1]" />
                    <RadzenDataGridColumn TItem="ResultObj" Visible=@IsVisibleBase[2] Property=@propertyBase[2] Title=@columnHeaderBase[2] Width="@columnWidthBase[2]" />
                    <RadzenDataGridColumn TItem="ResultObj" Visible=@IsVisibleBase[3] Property=@propertyBase[3] Title=@columnHeaderBase[3] Width="@columnWidthBase[3]" />
                    <RadzenDataGridColumn TItem="ResultObj" Visible=@IsVisibleBase[4] Property=@propertyBase[4] Title=@columnHeaderBase[4] Width="@columnWidthBase[4]">

As I noted in my first reply in my opinion you should create the columns in a loop.

How would I get rid of the existing columns when I wanted to make a new set? Will Dispose take care of that?

Check this thread: Radzen Grid : Creating and filling columns dynamically - #2 by korchev

Same issue. I have rewritten it as a loop and made a copy of the index variable and everything works great if I change the values in baseColumns except changing Property

                    @for (int i = 0; i < baseColumns.Length; i++)
                        int columnIndex = i;          
                        <RadzenDataGridColumn TItem="ResultObj" Property=@baseColumns[columnIndex].Property Visible=@baseColumns[columnIndex].Visible Width=@baseColumns[columnIndex].Width Title=@baseColumns[columnIndex].Title />