RadGrid for ASP.NET

Reordering columns Send comments on this topic.
See Also
Grid columns > Reordering columns

Glossary Item Box

You can set the order of the grid columns by dragging and dropping them. All you need to do is set the AllowColumnsReorder Property to true. There are two possible modes for column reordering: client and server-side. If you want to reorder columns on client, you need to set ReorderColumnsOnClient property to true. By default it is false and columns are reordered on the server.

Note that if you reorder columns on client, changes will be persisted on the server only after postback.
Please bear in mind that EnableColumnViewState property should be true (this is its default value).

Reorder Columns

Reordering columns programmatically

When columns are created programmatically, they appear in Telerik RadGrid in the same order as they were added to the Columns collection. You can reorder columns dynamically on the server-side using:

SwapColumns(String,String) Method which accepts the UniqueNames for the columns

C# Copy Code
grid.MasterTableView.SwapColumns("City","ContactName");

VB.NET Copy Code
grid.MasterTableView.SwapColumns("City","ContactName")



SwapColumns(Int32,Int32) Method which accepts the columns order indexes.

C# Copy Code
grid.MasterTableView.SwapColumns(3, 4);
VB.NET Copy Code
grid.MasterTableView.SwapColumns(3, 4)

Note that GridExpandColumn and GridRowIndicatorColumn are always in front of data columns, so your columns will start from index 2 (Columns[2]).

Another possible solution is to use the orderIndex property:

C# Copy Code
GridColumn c = new GridColumn();
c = g.MasterTableView.Columns.FindByUniqueName(columnName);
if (c != null){
 c.OrderIndex = nPosition++;}

 

VB.NET Copy Code
Dim c as GridColumn = new GridColumn()
c = g.MasterTableView.Columns.FindByUniqueName(columnName)
If (Not c Is Nothing) Then
  c.OrderIndex = nPosition + 1EndIf

What you should have in mind in this case is that it will work only for the first postback! It sets the new index to a column (let's say 4), but it doesn't set a new index to the column that had this index (4). So as a result you will have two columns with the same index (4).

Reordering columns in NoPersistance mode

Since RadGrid needs to rebind at each page load in order to maintain its state when working in NoPersistance mode (see ViewState optimization), the client-side column reordering is not working as expected. Telerik RadGrid does not currently provide a solution for this issue. You may try setting ReorderCoumnsOnClient to false when working with NoPersistence.

See Also

Performance tips and tricks
Optimizing ViewState usage