Sorting in Windows Forms Pivot Chart

13 Jul 20261 minute to read

The pivot chart supports sorting of both PivotAxis and PivotLegend programmatically in the ascending and descending order. By default, the pivot chart populates the series in ascending order.

Sort by pivot axis

The pivot axis can be sorted by adding a PivotSortDescriptor to the SortedAxis collection and ListSortDirection to specify the sorting order.

Here, “Gender” is a PivotAxis item.

//Adding PivotAxis to the SortAxis collection for sorting.
this.pivotChart1.SortedAxis.Add(new PivotSortDescriptor("Gender", ListSortDirection.Ascending));
'Adding PivotAxis to the SortAxis collection for sorting.
Me.pivotChart1.SortedAxis.Add(New PivotSortDescriptor("Gender", ListSortDirection.Ascending))

Before applying sorting:

WinForms PivotChart before sorting

After applying sorting on PivotAxis “Gender”:

WinForms PivotChart after sorting

Sort by pivot legends

PivotLegends can be sorted by adding a PivotSortDescriptor to the SortedLegends collection and ListSortDirection to specify the sorting order.

Here, the “State” of PivotLegends item is FieldMappingName and the sorting order is ascending.

//Adding PivotLegends to the SortLegends collection for sorting.
this.pivotChart1.SortedLegends.Add(new PivotSortDescriptor("State"));
'Adding PivotLegends to the SortLegends collection for sorting.
Me.pivotChart1.SortedLegends.Add(New PivotSortDescriptor("State"))

Events

There are two events available in both SortedAxis and SortedLegends of the pivot chart to handle or notify the sorting operation.

S.No Event name Details Event arguments
1 Changing Occurs before a property in a nested element or the collection is changed. PivotItemSortingEventArgs
2 Changed Occurs after a property in a nested element or the collection is changed. PivotItemSortedEventArgs