How to sort the groups based on records count in WinForms GridGroupingControl?
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 in WinForms GridGroupingControl.
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
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
Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/sorting
Conclusion
I hope you enjoyed learning about how to sort the groups based on record count in WinForms GridGroupingControl.
You can refer to our WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!