How to Achieve Numeric Column Sorting in Griddataboundgrid in Windows Forms GridDataBoundGrid(Classic)

To perform a custom sort through DataView, the object in the column should implement IComparer interface. Once this is done, DataView will use IComparer interface implementation to sort the column instead of using default sort of control.

public class StringLenComparer : IComparer
{
    #region IComparer Members
    public int Compare(object x, object y)
    {
        string value1 = x.ToString(); ;
        string value2 = y.ToString(); ;
        return  value1.Length - value2.Length;
    }
    #endregion
}
public class IntComparer : IComparer
{
    #region IComparer Members
    public int Compare(object x, object y)
    {
        if(x == null && y == null)
        return 0;
        else if(x == null)
        return -1;
        else if(y == null)
        return 1;
        else
        return int.Parse(x.ToString()) -  int.Parse(y.ToString());
    }
    #endregion
}
Public Class StringLenComparer Implements IComparer
#Region "IComparer Members"
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Dim value1 As String = x.ToString()
Dim value2 As String = y.ToString()
Return value1.Length - value2.Length
End Function
#End Region
End Class
Public Class IntComparer Implements IComparer
#Region "IComparer Members"
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
If x Is Nothing AndAlso y Is Nothing Then
Return 0
ElseIf x Is Nothing Then
Return -1
ElseIf y Is Nothing Then
Return 1
Else
Return Integer.Parse(x.ToString()) - Integer.Parse(y.ToString())
End If
End Function
#End Region
End Class

To view a sample on this, see Numeric Column Sorting.