Articles in this section
Category / Section

How to do custom grouping in WinForms GridGroupingControl?

3 mins read

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:

C#

//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;
        else
            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;
    }
    #endregion
}

VB

'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
         Else
               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:

C#

//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;
        else
        {
            int i = int.Parse(x.ToString());
            int j = int.Parse(y.ToString());
            return i - j;
        }
    }
}

VB

'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
        Else
            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.

C#

//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();
this.gridGroupingControl1.TableDescriptor.GroupedColumns.Add(cd);

VB

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

 

 

Show custom grouping in GridGroupingControl

Figure 1: custom grouping

Samples:

C#: CustomGrouping-CS.zip

VB: CustomGrouping-VB.zip

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied