Articles in this section
Category / Section

How to sort the month values of a pivot column or row in WinForms PivotGridControl?

2 mins read

Sort the pivot column or row values

By default, the PivotColumns or PivotRows are loaded in ascending order. So, the month values are loaded as April, August, February, etc., instead of January, February, March, etc., In order to sort the month values in default order, the IComparer interface can be implemented.

 

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 and the CustomComparer can be added to the PivotRows/PivotColumns by using Comparer field of PivotItem

C#

//Custom Comparer
public class MonthOrderComparer : IComparer
{
    #region IComparer Members   
    Dictionary<string, int> month;
    public MonthOrderComparer(Dictionary<string, int> month)
    {
        this.month = month;
    }
    public int Compare(object x, object y)
    {
        if (x == null && y == null)
            return 0;
        else if (y == null)
            return 1;
        else if (x == null)
            return -1;
        else
        {
            string keyX = x.ToString();
            string keyY = y.ToString();
            if (month[keyX] < month[keyY])
            {
                return 0;
            }
            else
                return month[keyX];
        }
    }
    #endregion
}
 
//Adding custom comparer to PivotRows
this.pivotGridControl1.PivotRows.Add(new PivotItem { FieldMappingName = "Date", TotalHeader = "Total", Comparer = new MonthOrderComparer(month) });
 
//Adding custom comparer to PivotColumns
this.pivotGridControl1.PivotColumns.Add(new PivotItem { FieldMappingName = "Date", TotalHeader = "Total", Comparer = new MonthOrderComparer(month) });

VB

'Custom Comparer
Public Class MonthOrderComparer
    Implements IComparer
    #Region "IComparer Members   "
    Private month As Dictionary(Of String, Integer)
    Public Sub New(ByVal month As Dictionary(Of String, Integer))
        Me.month = month
    End Sub
    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 y Is Nothing Then
            Return 1
        ElseIf x Is Nothing Then
            Return -1
        Else
            Dim keyX As String = x.ToString()
            Dim keyY As String = y.ToString()
            If month(keyX) < month(keyY) Then
                Return 0
            Else
                Return month(keyX)
            End If
        End If
    End Function
    #End Region
End Class
 
'Adding custom comparer to PivotRows
Me.pivotGridControl1.PivotRows.Add(New PivotItem With {.FieldMappingName = "Date", .TotalHeader = "Total", .Comparer = New MonthOrderComparer (month)})
 
'Adding custom comparer to PivotColumns
Me.pivotGridControl1.PivotColumns.Add(New PivotItem With {.FieldMappingName = "Date", .TotalHeader = "Total", .Comparer = New MonthOrderComparer (month)})

 

Screenshot

Sort the pivot columns or rows values

 

Samples:

C#: CustomSorting_CS

VB: CustomSorting_VB

Reference link:

1. https://help.syncfusion.com/windowsforms/pivot-grid/pivot-columns

2. https://help.syncfusion.com/windowsforms/pivot-grid/pivot-rows

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