Skip to main content

PivotGridControl.CustomServerModeSort Event

Allows you to implement a custom sorting algorithm for the field values in OLAP and server modes.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.XtraPivotGrid.v23.2.dll

NuGet Package: DevExpress.Win.PivotGrid

Declaration

public event EventHandler<CustomServerModeSortEventArgs> CustomServerModeSort

Event Data

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

Property Description
Field Gets the field being processed. Inherited from PivotFieldEventArgsBase<T>.
IsFilterPopupSorting Get the value that indicates whether the PivotGridControl.CustomServerModeSort event is raised. Inherited from CustomServerModeSortEventArgsBase<T>.
OLAPMember1 Get the first member being compared. Inherited from CustomServerModeSortEventArgsBase<T>.
OLAPMember2 Get the second member being compared. Inherited from CustomServerModeSortEventArgsBase<T>.
Result Gets or sets the result of a custom comparison. Inherited from CustomServerModeSortEventArgsBase<T>.
Value1 Gets the first value being compared. Inherited from CustomServerModeSortEventArgsBase<T>.
Value2 Gets the second value being compared. Inherited from CustomServerModeSortEventArgsBase<T>.

The event data class exposes the following methods:

Method Description
GetCellValue1(CrossAreaKey, PivotGridFieldBase) In OLAP and server mode, returns the first cell value calculated for the specified cross area key against the specified data field. Inherited from CustomServerModeSortEventArgsBase<T>.
GetCellValue1(Object[], PivotGridFieldBase) In OLAP and server mode, returns the first cell value calculated for the specified cross area field values against the specified data field. Inherited from CustomServerModeSortEventArgsBase<T>.
GetCellValue2(CrossAreaKey, PivotGridFieldBase) In OLAP and server mode, returns the second cell value calculated for the specified cross area key against the specified data field. Inherited from CustomServerModeSortEventArgsBase<T>.
GetCellValue2(Object[], PivotGridFieldBase) In OLAP and server mode, returns the second cell value calculated for the specified cross area field values against the specified data field. Inherited from CustomServerModeSortEventArgsBase<T>.
GetCrossAreaKey(Object[]) In OLAP and server mode, returns cross area values by which you want to sort the pivot grid column or row. Inherited from CustomServerModeSortEventArgsBase<T>.

Remarks

Handle the CustomServerModeSort event to implement a custom sorting algorithm in OLAP and server modes for a specific field. Set the PivotGridFieldBase.SortMode property of the sorted field to Custom to apply custom sorting. Otherwise, the CustomServerModeSort event is not fired.

Use the PivotFieldEventArgsBase<T>.Field property to specify the field being processed.

Create an instance of the CrossAreaKey class to specify the cross area of field values by which you want to sort the data. Call the CustomServerModeSortEventArgsBase<T>.GetCrossAreaKey method to assign field values to the created CrossAreaKey object.

Use the CustomServerModeSortEventArgsBase<T>.GetCellValue1 and CustomServerModeSortEventArgsBase<T>.GetCellValue2 methods to get cell values.

In the event handler, compare cell values and assign the result to the CustomServerModeSortEventArgsBase<T>.Result property.

  • Set the value to -1 to make the current object precede the compared object in the sort order.
  • Set the value to 0 to leave the current object in the same position as the compared object.
  • Set the value to 1 to make the current object follow the compared object in the sort order.

In OLAP, the members to be compared are specified by the CustomServerModeSortEventArgsBase<T>.OLAPMember1 and CustomServerModeSortEventArgsBase<T>.OLAPMember2 properties.

To sort data with the OLAP member properties, set the PivotGridFieldBase.SortMode property of the sorted field to DimensionAttribute and use the PivotGridFieldBase.SortByAttribute property to sort data.

In OLAP mode, when you get the IOLAPMember.Properties property, the Pivot Grid sends a query to the server every time for each member. You can use the PivotGridFieldBase.AutoPopulatedProperties to specify the list of OLAP member properties which should be returned with a data query from the server. Create a new collection of OLAP member attributes with the PivotGridFieldBase.AutoPopulatedProperties property to improve performance.

To get the OLAP member properties values, use the IOLAPMember.AutoPopulatedProperties property.

Example: Custom Sorting in OLAP Mode

This example demonstrates how to handle the CustomServerModeSort event to sort data by the e.OLAPMember1 and e.OLAPMember2 properties.

View Example

In this example, the Product field values are sorted by the “List Price” OLAP member property. The “List Price” member property values are displayed near the Product field values.

OLAP custom sorting

using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using DevExpress.Data.Filtering;
using System.Collections;
using System.Linq;
using System;

namespace WinFormsPivotGridCustomOLAPSort {
    public partial class Form1 : Form {
        public Form1() {

            InitializeComponent();
            // Creates a new collection of OLAP member properties.
            fieldProduct.AutoPopulatedProperties = new string[] { "Color", "Class", "List Price" };
            //Sets a field's sort mode to Custom to raise the CustomServerModeSort event.
            fieldProduct.SortMode = PivotSortMode.Custom;
            fieldFiscalYear.FilterValues.FilterType = PivotFilterType.Included;
            fieldFiscalYear.FilterValues.ValuesIncluded = new object[] { 2004, 2005 };
            pivotGridControl1.BestFit();

        }

        private void pivotGridControl1_CustomServerModeSort(object sender, 
            CustomServerModeSortEventArgs e) {
            if (e.Field == fieldProduct) {
                // Sets the result of comparing the "Product" field's values 
                // by the "Color" OLAP member property.
                e.Result = Comparer.Default.Compare(
                    e.OLAPMember1.AutoPopulatedProperties["List Price"].Value,
                    e.OLAPMember2.AutoPopulatedProperties["List Price"].Value
                );
            }
        }

        private void pivotGridControl1_FieldValueDisplayText(object sender,
            PivotFieldDisplayTextEventArgs e) {
            if (e.Field == fieldProduct) {
                IOLAPMember currentMember =
                   e.Field.GetOLAPMembers().First(m => Object.Equals(m.Value, e.Value));
                e.DisplayText +=
                   string.Format(" ({0:C2})", currentMember.AutoPopulatedProperties["List Price"].Value);
            }
        }
    }
}
See Also