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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to custom sort a string column so that all the empty cells in the WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl
Tags: sorting, custom

Custom sorting


By default, empty cells are included while sorting in the GridGroupingControl. To keep these empty cells always at the bottom while sorting, you have to implement an IComparer interface.


This customized sorting can be done by having a user-defined class that implements the IComparer interface and writing the custom comparer code in the Compare() method. Excluding the empty cells can be done by finding the length of the string in the Compare() method and cancelling its sorting, thereby pushing the empty cells to the bottom. Refer to the following code for implementing the Compare() method.


public class customsort : IComparer
    public int Compare(object x, object y)
        if(x == null && y == null)
           return 0;
        // Does not include empty strings for comparison.
        else if (x == System.DBNull.Value)
           return 0;
        else if (y == System.DBNull.Value)
           return 0;
           return ((IComparable)x.ToString()).CompareTo(y.ToString());


Public Class customsort Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
        If x Is Nothing AndAlso y Is Nothing Then
           Return 0
        'Does not include empty strings for comparison.
        ElseIf x Is System.DBNull.Value Then
           Return 0
        ElseIf y Is System.DBNull.Value Then
           Return 0
           Return (CType(x.ToString(), IComparable)).CompareTo(y.ToString())
        End If
    End Function

You can create an instance of the SortColumnDescriptor for a particular column and set the Comparer() method to the instance of the inherited IComparer class. You can also initialize its sorting direction by using Sort Direction property.


//Sets the columnname to be sorted.
SortColumnDescriptor cd = new Syncfusion.Grouping.SortColumnDescriptor("City");
//Sets the sortdirection.
cd.SortDirection = System.ComponentModel.ListSortDirection.Descending;
//Handles the empty cells at the bottom.
cd.Comparer = new customsort();


'Sets the columnname to be sorted.
Dim cd As SortColumnDescriptor = New Syncfusion.Grouping.SortColumnDescriptor("City")
'Sets the sortdirection.
cd.SortDirection = System.ComponentModel.ListSortDirection.Descending
'Handles the empty cells at the bottom.
cd.Comparer = New customsort() 


C#: Custom sorting

VB: Custom sorting

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

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