I have a radgrid that uses a combination of Autogenerated columns (because the columns cannot be identified until the query is created) and gridtemplate columns.
My main problem is that the gridtemplate columns are first before the autogenerated columns. I'd like to find a way to make the gridtemplate columns be last.
I tried to attempt this using MasterTableView.SwapColumns but i dont want to swap. Instead i just want to move all gridtemplate columns to be last. Im attempting this code in my radgrid_prerender.
Also the order that the autogenerated columns are created must remain in that order because the sql query is built based on what the user has selected as fields to display.
Example ("select field1, field2, field3, field4 from table") it should stay in that order for autogenerated columns without having to swap any.
Hope this makes sense
thank you,
Mike
5 Answers, 1 is accepted

You can set the OrderIndex property of the columns, instead of using SwapColumns(). Set the OrderIndex for each columns from code behind and call Rebind() method of grid.
I hope the documentation will be helpful to you.
Reordering columns
Here is the code that I tried for same kind of scenario:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
GridColumn c = RadGrid1.MasterTableView.GetColumnSafe("TemplateColumnUniqueName"); |
if (c != null) |
{ |
int total = RadGrid1.MasterTableView.RenderColumns.Count() - 2; |
for (int i = total; i >= 2; i--) |
{ |
RadGrid1.MasterTableView.RenderColumns[i + 1].OrderIndex = i - 1; |
} |
c.OrderIndex = total + 1; |
} |
RadGrid1.MasterTableView.Rebind(); |
} |
[Note: When using the OrderIndex property to reorder columns, make sure that you assign values so that no two columns have the same index and no index is omitted.]
Regards,
Shinu.

The version of Telerik Im using is 2008.2.723.20.
RadGrid1.MasterTableView.RenderColumns.Count does not seem to be available. I wonder if this is only in latest versions.
Thanks for the link for the documentation I did look at it previously and saw the orderindex but I could not find a way to loop through the autogenerated columns to re-order their indexes. Only the gridtemplate columns were part of the gridcolumncollection for the sample code
Dim cols As GridColumnCollection = rgAdhocReport.MasterTableView.Columns |
Dim c As GridColumn = cols.FindByUniqueName("ACTIONS") |
If Not IsNothing(c) Then |
Dim start As Integer = c.OrderIndex |
Dim i As Integer = start |
While i < cols.Count |
c = cols(i) |
If i < cols.Count - 1 Then |
c.OrderIndex = i + 1 |
Else |
c.OrderIndex = start |
End If |
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1) |
End While |
End If |


Hi Shinu,
But Rebind in Prerender does not work. Any other alternate suggestion please?
-Thanks
Gayathri

My problem is as follows.
I have 5 columns added at design time. A,B,C,D,E(These are Gridboundcolumn)
and finally i am adding H as gridbutton column with delete button with delete image. all are done nicely. But F and G are added dynamically and H is placed in between this F & G which i want to move after G.
Unfortunately reordering in prerender and rebind of the grid does not work .
Please suggest.
-Thanks
Dim count As Integer = gridRecords.MasterTableView.RenderColumns.Length
Dim cols As GridColumnCollection = gridRecords.MasterTableView.Columns
Dim C As GridTemplateColumn = cols.FindByUniqueName("recordDelete")
C.OrderIndex = count + 1
gridRecords.Rebind()
Used in a Sub to add data to the grid