Skip to main content

PivotGridControl.CustomFieldSort Event

Allows you to implement a custom sorting algorithm for field values.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.XtraPivotGrid.v23.2.dll

NuGet Package: DevExpress.Win.PivotGrid

Declaration

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
Data For internal use. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
Field Gets the field whose values are being compared. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
Handled Gets or sets whether a comparison operation is being handled, so no default processing is required. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
ListSourceRowIndex1 Gets the index in the data source for the first of the two rows being compared. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
ListSourceRowIndex2 Gets the index in the data source for the second of the two rows being compared. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
Result Gets or sets the result of a custom comparison. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
SortLocation Gets a target UI element to whose values sorting is applied. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
SortOrder Gets the sort order applied to the field. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
Value1 Gets the first value being compared. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
Value2 Gets the second value being compared. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.

The event data class exposes the following methods:

Method Description
GetListSourceColumnValue(Int32, String) Gets a value from the specified row and column. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
GetSortResult() For internal use. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.
SetArgs(ICustomSortRowsEventArgs) Initializes the PivotGridCustomFieldSortEventArgsBase<T> instance with values obtained from the specified object. For internal use. Inherited from PivotGridCustomFieldSortEventArgsBase<T>.

Remarks

The CustomFieldSort event occurs for the field whose PivotGridFieldBase.SortMode property is set to the PivotSortMode.Custom value. Handle the CustomFieldSort event to implement a custom sorting algorithm for the field values.

In OLAP or server mode, handle the PivotGridControl.CustomServerModeSort event instead.

Important

The CustomFieldSort event is not supported in Optimized mode.

Refer to the following example for information on how to use a hidden field to sort the visible field values when Pivot Grid operates in Optimized mode:

View Example: How to Use a Hidden Field to Sort the Visible Field

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, follow the steps listed below.

  1. Get values to compare from the e.Value1 and e.Value2 properties.
  2. Compare these values and assign the result to the e.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 e.GetListSourceColumnValue method with the following parameters:

  3. Set the e.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.

Note

The CustomFieldSort event occurs in different situations in PivotDataProcessingEngine.Legacy and PivotDataProcessingEngine.LegacyOptimized modes because of the different workflow - the Legacy engine aggregates data after sorting is done at the data source level, while the LegacyOptimized engine sorts the aggregated data. The Legacy engine raises the CustomFieldSort event to compare different rows of data. The LegacyOptimized engine raises the event to compare the resulting data groups.

You can use the e.ListSourceRowIndex1 and e.ListSourceRowIndex2 properties and the e.GetListSourceColumnValue method. They return the data from the first data row included in the processed group.

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 e.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 a field in the Drop-Down Filter.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomFieldSort event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also