)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to sort the groups based on records count in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: March 15, 2017 |
Last Revised Date: December 23, 2019

Sorting

To sort the grouped items by its record count, CustomGroupSortOrderComparer class which is derived from IGroupSortOrderComparer interface can be created and customize the Compare method of custom comparer.

Creating CustomGroupSortOrderComparer

C#

//CustomGroupSortOrderComparer
public class CustomGroupSortOrderComparer : IGroupSortOrderComparer
 {
     ListSortDirection sortDirection;
     public CustomGroupSortOrderComparer(ListSortDirection sortDirecti
     {
         this.sortDirection = sortDirection;
     }
     public int Compare(object x, object y)
     {
         Group gx = (Group)x;
         Group gy = (Group)y;
         //To get the Record Count of Grouped items
         object vx = gx.GetRecordCount();
         object vy = gy.GetRecordCount();
         if (sortDirection == ListSortDirection.Ascending)
             return ((IComparable)vx).CompareTo(vy);
         else
             return ((IComparable)vy).CompareTo(vx);
     }
     public string[] GetDependantFields(TableDescriptor td)
     {
         return new string[0];
     }
 }

 

VB

'CustomGroupSortOrderComparer
Public Class CustomGroupSortOrderComparer
    Implements IGroupSortOrderComparer
    Private sortDirection As ListSortDirection
    Public Sub New(ByVal sortDirection As ListSortDirection)
        Me.sortDirection = sortDirection
    End Sub
    Private Function IComparer_Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Dim gx As Group = CType(x, Group)
        Dim gy As Group = CType(y, Group)
        'To get the RecordCount of Grouped items
        Dim vx As Object = gx.GetRecordCount()
        Dim vy As Object = gy.GetRecordCount()
        If sortDirection = ListSortDirection.Ascending Then
            Return (CType(vx, IComparable)).CompareTo(vy)
        Else
            Return (CType(vy, IComparable)).CompareTo(vx)
        End If
    End Function
    Private Function IGroupSortOrderComparer_GetDependantFields(td As TableDescriptor) As String() Implements IGroupSortOrderComparer.GetDependantFields
        Return New String() {}
    End Function

 

Setting CustomGroupSortOrderComparer

CustomGroupSortOrderComparer has to be set to GroupSortOrderComparer property of SortColumnDescriptor that you want to sort.

C#

//To Group the Column
SortColumnDescriptor cd = new Syncfusion.Grouping.SortColumnDescriptor("CategoryName");
 
//To add Grouped columns to GridGroupingControl
this.gridGroupingControl1.TableDescriptor.GroupedColumns.Add(cd);
 
//To set CustomComparer to SortColumnDescriptor
cd.GroupSortOrderComparer = new CustomGroupSortOrderComparer(ListSortDirection.Descending);

 

VB

'To Group the Column
Dim cd As SortColumnDescriptor = New Syncfusion.Grouping.SortColumnDescriptor("CategoryName")
 
'To add Grouped columns to GridGroupingControl
Me.gridGroupingControl1.TableDescriptor.GroupedColumns.Add(cd)
 
'To set CustomGroupSortOrderComparer to SortColumnDescriptor
cd.GroupSortOrderComparer = New CustomGroupSortOrderComparer(ListSortDirection.Descending)

 

 

Screenshot

Group sorting based on the record count

 

Note:

If you want to sort the all other columns, the custom comparer has to be set for all other column’s SortColumnDescriptor as like above.

Sample Link

C#: CustomGroupSorting_CS

VB: CustomGroupSorting_VB

Reference link: https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/sorting

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon