Skip to main content

ASPxPivotGrid.CustomFieldSort Event

Provides the ability to sort data using custom rules.

Namespace: DevExpress.Web.ASPxPivotGrid

Assembly: DevExpress.Web.ASPxPivotGrid.v23.1.dll

NuGet Package: DevExpress.Web


public event PivotGridCustomFieldSortEventHandler CustomFieldSort

Event Data

The CustomFieldSort event's data class is PivotGridCustomFieldSortEventArgs. The following properties provide information specific to this event:

Property Description
Field Gets the field whose values are being compared.
Handled Gets or sets whether a comparison operation is being handled and therefore no default processing is required.
ListSourceRowIndex1 Gets the index in the data source of the first of the two rows being compared.
ListSourceRowIndex2 Gets the index in the data source of the second of the two rows being compared.
Result Gets or sets the result of a custom comparison.
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 event data class exposes the following methods:

Method Description
GetListSourceColumnValue(Int32, String) Gets the specified field’s value in the specified row in the control’s underlying data source.


Handle the CustomFieldSort event to provide a custom sorting algorithm for a specific field. Note that the PivotGridFieldBase.SortMode property of this field should be set to PivotSortMode.Custom to apply custom sorting. Otherwise, the CustomFieldSort event will not be raised.


The CustomFieldSort event is not supported in Optimized mode.

The CustomFieldSort event fires for pairs of rows in the underlying data source, before data is grouped according to the layout of column and row fields. The PivotGridCustomFieldSortEventArgs.ListSourceRowIndex1 and PivotGridCustomFieldSortEventArgs.ListSourceRowIndex2 parameters identify the indexes of the rows. The field being processed is specified by the PivotGridCustomFieldSortEventArgs.Field parameter. The field’s values in these rows are specified by the PivotGridCustomFieldSortEventArgs.Value1 and PivotGridCustomFieldSortEventArgs.Value2 parameters.

In the event handler, you should compare these two values and assign the result to the PivotGridCustomFieldSortEventArgs.Result property as follows:

  • set Result to -1, if the first row should be positioned above the second row, when sorting data in ascending order. When data is sorted in descending order, the first row will be positioned below the second row.
  • set Result to 1, if the first row should be positioned below the second row, when sorting data in ascending order. When data is sorted in descending order, the first row will be positioned above the second row.
  • set Result to 0 to indicate that the rows are equal. In this case, the compared rows will be grouped into one field value.

If your comparison logic requires additional data from the underlying data source, use the PivotGridCustomFieldSortEventArgs.GetListSourceColumnValue method. This method allows you to obtain values from the data source by a column name and row index.

The PivotGridCustomFieldSortEventArgs.Handled property must be set to true, if the current comparison operation was handled. You can leave this parameter set to false, to invoke the default comparison mechanism after your event handler has finished. In the latter instance, the result of a custom comparison operation is ignored.

The CustomFieldSort event also fires to sort unique filter values in the Filter Window. Each filter value might correspond to multiple rows in the underlying data source. So, the event’s ListSourceRowIndex1 and ListSourceRowIndex2 parameters cannot be used to identify underlying rows. They are set to -1, when comparing filter values in the Filter Window.


Custom sorting is not supported in OLAP or server mode. To sort data in OLAP and server mode using custom algorithms, handle the ASPxPivotGrid.CustomServerModeSort event.


This example demonstrates how to implement a custom sorting algorithm by handling the ASPxPivotGrid.CustomFieldSort event.

The image shows the result:


using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;
// ...

protected void Page_Load(object sender, EventArgs e) {  
    if(!IsPostBack && !IsCallback) {
        // fieldResult  
        fieldResult.SortMode = PivotSortMode.Custom;

protected void pivotGrid_CustomFieldSort(object sender, PivotGridCustomFieldSortEventArgs e) {
    if (e.Field.FieldName == "Result") {
        if (e.Value1 == null || e.Value2 == null) return;
        e.Handled = true;
        string s1 = e.Value1.ToString().Replace("Result ", String.Empty);
        string s2 = e.Value2.ToString().Replace("Result ", String.Empty);
        if (Convert.ToInt32(s1) > Convert.ToInt32(s2))
            e.Result = 1;
            if (Convert.ToInt32(s1) == Convert.ToInt32(s2))
            e.Result = System.Collections.Generic.Comparer<Int32>.Default.Compare(Convert.ToInt32(s1),
            e.Result = -1;
See Also