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 do custom grouping in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: April 30, 2011 |
Last Revised Date: February 13, 2020
Tags: grouping

Custom grouping

A more general way of categorizing records is to add own custom categorizer object to the SortColumnDescriptor that defines the group. You can also add a custom comparer object to the SortColumnDescriptor. When a column is grouped, it is first sorted. The Comparer object allows you to control how the sorting is done on your column. Once the column is sorted, the custom categorizer is used to determine the adjacent records in the sorted column that belong to the same group. To create custom comparer and categorizer objects, you can define classes that implement either IComparer (one method) or ICategorizer (two methods).

Custom Categorizer:


//defines custom categorizer
public class CustomCategorizer : Syncfusion.Grouping.IGroupByColumnCategorizer
    //defines a group and returns a group category object (here returns 1 through 5)
    public static int GetCategory(int i)
        int ret = 0;
        if (i < 10)
            ret = 1;
        else if (i >= 10 && i < 20)
            ret = 2;
        else if (i >= 20 && i < 30)
            ret = 3;
        else if (i >= 30 && i < 40)
            ret = 4;
            ret = 5;
        return ret;
    public object GetGroupByCategoryKey(SortColumnDescriptor column, bool isForeignKey, Record record)
        return GetCategory(int.Parse(record.GetValue(column).ToString()));
    public int CompareCategoryKey(SortColumnDescriptor column, bool isForeignKey, object category, Record record)
        return GetCategory(int.Parse(record.GetValue(column).ToString())) - (int)category;


'defines custom categorizer
Public Class CustomCategorizer  Implements Syncfusion.Grouping.IGroupByColumnCategorizer
    'defines a group and returns a group category object (here returns 1 through 5)
    Public Shared Function GetCategory(ByVal i As Integer) As Integer
         Dim ret As Integer = 0
         If i < 10 Then
              ret = 1
         ElseIf i >= 10 AndAlso i < 20 Then
              ret = 2
         ElseIf i >= 20 AndAlso i < 30 Then
               ret = 3
         ElseIf i >= 30 AndAlso i < 40 Then
               ret = 4
               ret = 5
         End If
         Return ret
    End Function
    Public Function GetGroupByCategoryKey(ByVal column As SortColumnDescriptor, ByVal isForeignKey As Boolean, ByVal record As Record) As Object
         Return GetCategory(Integer.Parse(record.GetValue(column).ToString()))
    End Function
    Public Function CompareCategoryKey(ByVal column As SortColumnDescriptor, ByVal isForeignKey As Boolean, ByVal category As Object, ByVal record As Record) As Integer
         Return GetCategory(Integer.Parse(record.GetValue(column).ToString())) - CInt(Fix(category))
    End Function
End Class

Custom Comparer class:


//make sure the string integers are sorted as integers instead of strings
public class CustomComparer : IComparer
    public int Compare(object x, object y)
        if (x == null)
            return -1;
        else if (y == null)
            return 100;
            int i = int.Parse(x.ToString());
            int j = int.Parse(y.ToString());
            return i - j;


'make sure the string integers are sorted as integers instead of strings
Public Class CustomComparer
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
        If x Is Nothing Then
            Return -1
        ElseIf y Is Nothing Then
            Return 100
            Dim i As Integer = Integer.Parse(x.ToString())
            Dim j As Integer = Integer.Parse(y.ToString())
             Return i - j
        End If
    End Function
End Class

In the Form_Load event, you can add the categorizer to the grouped columns.


//group "Col2" using a custom categorizer and Comparer
Syncfusion.Grouping.SortColumnDescriptor cd = new Syncfusion.Grouping.SortColumnDescriptor("Col2");
cd.Categorizer = new CustomCategorizer();
cd.Comparer = new CustomComparer();


'group "Col2" using a custom categorizer and Comparer
Dim cd As New Syncfusion.Grouping.SortColumnDescriptor("Col2")
cd.Categorizer = New CustomCategorizer()
cd.Comparer = New CustomComparer()



Show custom grouping in GridGroupingControl

Figure 1: custom grouping


C#: CustomGrouping-CS.zip

VB: CustomGrouping-VB.zip

Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/grouping#custom-grouping

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
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