The event handler receives an argument of type PivotGridCustomFieldSortEventArgs containing data related to this event.
The following
PivotGridCustomFieldSortEventArgs properties provide information specific to this event.
Property |
Description |
Data |
|
Field |
Gets the field whose values are being compared. |
Handled |
|
ListSourceRowIndex1 |
Gets the index in the data source for the first of the two rows being compared. |
ListSourceRowIndex2 |
Gets the index in the data source for the second of the two rows being compared. |
Result |
|
SortLocation |
Gets a target UI element to whose values sorting is applied. |
SortOrder |
Gets the sort order applied to the field. |
Value1 |
Gets the first value being compared. |
Value2 |
Gets the second value being compared. |
The CustomFieldSort event occurs for the field whose DevExpress.XtraPivotGrid.PivotGridFieldBase.SortMode property is set to the DevExpress.XtraPivotGrid.PivotSortMode.Custom value. Handle the CustomFieldSort event to provide a custom sorting algorithm for the field values.
How to Sort Field Values in Pivot Grid
The CustomFieldSort event fires for pairs of field values before they are grouped according to the layout of column and row fields. To establish a new sort order, perform the following actions:
- Get values to compare from the DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.Value1 and DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.Value2 properties.
-
Compare these values and assign the result to the DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.Result property as follows:
- -1 if the first value should be placed before the second value when values are sorted in ascending order. When values are sorted in descending order, the first value is after the second value.
- 1 if the first value should be placed after the second value when values are sorted in ascending order. When values are sorted in descending order, the first value is placed before the second value.
- 0 to indicate that the values are equal. The rows are grouped into one field value.
Note
Values are sorted before Grouping. If the values are equal (the e.Result is set to 0), they are in the same group.
To get additional field values from the data source, call the DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.GetListSourceColumnValue method with the following parameters:
-
Set the DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.Handled property to true to finalize the comparison and use the e.Result value. Otherwise, the default comparison is in effect and e.Result is ignored.
How to Sort Filter Values in Filter Drop-Down
Handle the CustomFieldSort event to sort unique filter values in the Filter Drop-Down. To determine whether the data comes from the filter, check the DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.SortLocation property - its value is PivotSortLocation.Filter or PivotSortLocation.GroupFilter.
Important
A field value in a filter relates to multiple rows in the underlying data source. The ListSourceRowIndex1 and ListSourceRowIndex2 properties are always -1 when the event fires for the field located in the Filter Drop-Down.

Example
This example demonstrates how to sort a field (Sales Person) by the hidden data field values (Last Name) in the pivot's column header (the field's SortLocation (see DevExpress.XtraPivotGrid.PivotGridCustomFieldSortEventArgsBase<T>.SortLocation) is DevExpress.XtraPivotGrid.PivotSortLocation.Pivot ). If a field is located in a filter popup, a custom comparison method is used to to sort the field values.

C# |
using DevExpress.XtraEditors;
using DevExpress.XtraPivotGrid;
using System;
using System.Collections;
namespace CustomFieldSortExample
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
pivotGridControl1.CustomFieldSort += new PivotGridCustomFieldSortEventHandler(pivotGridControl1_CustomFieldSort);
}
private void Form1_Load(object sender, EventArgs e)
{
excelDataSource1.FileName = "SalesPerson.xlsx";
excelDataSource1.Fill();
pivotGridControl1.BestFit();
}
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
fieldSalesPerson1.SortMode = ((CheckEdit)sender).Checked ? PivotSortMode.Custom : PivotSortMode.Default;
}
void pivotGridControl1_CustomFieldSort(object sender, PivotGridCustomFieldSortEventArgs e)
{
if (e.Field.FieldName == "Sales Person")
{
if (e.SortLocation == PivotSortLocation.Pivot)
{
object orderValue1 = e.GetListSourceColumnValue(e.ListSourceRowIndex1, "Last Name"),
orderValue2 = e.GetListSourceColumnValue(e.ListSourceRowIndex2, "Last Name");
e.Result = Comparer.Default.Compare(orderValue1, orderValue2);
}
else
{
e.Result = Comparer.Default.Compare(e.Value1.ToString().Split(' ')[1], e.Value2.ToString().Split(' ')[1]);
}
e.Handled = true;
}
}
}
}
|
VB |
Imports DevExpress.XtraEditors
Imports DevExpress.XtraPivotGrid
Imports System
Imports System.Collections
Namespace CustomFieldSortExample
Partial Public Class Form1
Inherits XtraForm
Public Sub New()
InitializeComponent()
AddHandler pivotGridControl1.CustomFieldSort, AddressOf pivotGridControl1_CustomFieldSort
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
excelDataSource1.FileName = "SalesPerson.xlsx"
excelDataSource1.Fill()
pivotGridControl1.BestFit()
End Sub
Private Sub checkEdit1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles checkEdit1.CheckedChanged
fieldSalesPerson1.SortMode = If(DirectCast(sender, CheckEdit).Checked, PivotSortMode.Custom, PivotSortMode.Default)
End Sub
Private Sub pivotGridControl1_CustomFieldSort(ByVal sender As Object, ByVal e As PivotGridCustomFieldSortEventArgs)
If e.Field.FieldName = "Sales Person" Then
If e.SortLocation = PivotSortLocation.Pivot Then
Dim orderValue1 As Object = e.GetListSourceColumnValue(e.ListSourceRowIndex1, "Last Name"), orderValue2 As Object = e.GetListSourceColumnValue(e.ListSourceRowIndex2, "Last Name")
e.Result = Comparer.Default.Compare(orderValue1, orderValue2)
Else
e.Result = Comparer.Default.Compare(e.Value1.ToString().Split(" "c)(1), e.Value2.ToString().Split(" "c)(1))
End If
e.Handled = True
End If
End Sub
End Class
End Namespace
|