)
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 sort the month values of a pivot column or row in WinForms PivotGridControl?

Platform: WinForms |
Control: PivotGridControl |
Published Date: November 15, 2017 |
Last Revised Date: January 7, 2020
Tags: sorting

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

2X faster development

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