Sorting group rows
By default, when you perform grouping, RadGridView sorts the created group rows alphabetically. This article demonstrates how to customize the groups sort order.
Consider the RadGridView is bound to a list of custom objects. If you group by DepartmentId you will notice that the group rows are sorted alphabetically as this property is typeof(string).
However, you can change this sort order by using a group comparer. It is necessary to create a class that implements the IComparer
Custom group comparer
public class GroupComparer : IComparer<Group<GridViewRowInfo>>
{
public int Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
{
int parsedX;
int parsedY;
if (int.TryParse(((object[])x.Key).First().ToString(), out parsedX) &&
int.TryParse(((object[])y.Key).First().ToString(), out parsedY))
{
int result = parsedX.CompareTo(parsedY);
DataGroup xGroup = x as DataGroup;
if (xGroup != null && ((DataGroup)x).GroupDescriptor.GroupNames.First().Direction == ListSortDirection.Descending)
{
result *= -1;
}
return result;
}
return ((object[])x.Key)[0].ToString().CompareTo(((object[])y.Key)[0].ToString());
}
}
Public Class GroupComparer
Implements IComparer(Of Group(Of GridViewRowInfo))
Public Function [Compare](x As Group(Of GridViewRowInfo), y As Group(Of GridViewRowInfo)) As Integer _
Implements IComparer(Of Group(Of GridViewRowInfo)).[Compare]
Dim parsedX As Integer
Dim parsedY As Integer
If Integer.TryParse(DirectCast(x.Key, Object()).First().ToString(), parsedX) AndAlso Integer.TryParse(DirectCast(y.Key, Object()).First().ToString(), parsedY) Then
Dim result As Integer = parsedX.CompareTo(parsedY)
Dim xGroup As DataGroup = TryCast(x, DataGroup)
If xGroup IsNot Nothing AndAlso DirectCast(x, DataGroup).GroupDescriptor.GroupNames.First().Direction = ListSortDirection.Descending Then
result *= -1
End If
Return result
End If
Return DirectCast(x.Key, Object())(0).ToString().CompareTo(DirectCast(y.Key, Object())(0).ToString())
End Function
End Class
The last thing you need to do is to replace the default MasterTemplate.GroupComparer with your custom one:
Custom group comparer
this.radGridView1.MasterTemplate.GroupComparer = new GroupComparer();
Me.RadGridView1.MasterTemplate.GroupComparer = New GroupComparer()