Back to Devexpress

PivotGridControl.CustomServerModeSort Event

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotgridcontrol-519997f2.md

latest9.8 KB
Original Source

PivotGridControl.CustomServerModeSort Event

In OLAP and server mode, provides the capability to sort data using custom rules.

Namespace : DevExpress.Xpf.PivotGrid

Assembly : DevExpress.Xpf.PivotGrid.v25.2.dll

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public event EventHandler<CustomServerModeSortEventArgs> CustomServerModeSort
vb
Public Event CustomServerModeSort As EventHandler(Of CustomServerModeSortEventArgs)

Event Data

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

PropertyDescription
FieldGets the field whose values are being compared.
OlapMember1Get the first member being compared.
OlapMember2Get the second member being compared.
ResultGets or sets the result of a custom comparison.
ThreadSafeField
Value1Gets the first value being compared.
Value2Gets the second value being compared.

The event data class exposes the following methods:

MethodDescription
GetCellValue1(CrossAreaKey, PivotGridField)In OLAP and server mode, returns the first cell value calculated for the specified cross area key against the specified data field.
GetCellValue1(Object[], PivotGridField)In OLAP and server mode, returns the first cell value calculated for the specified cross area field values against the specified data field.
GetCellValue2(CrossAreaKey, PivotGridField)In OLAP and server mode, returns the second cell value calculated for the specified cross area key against the specified data field.
GetCellValue2(Object[], PivotGridField)In OLAP and server mode, returns the second cell value calculated for the specified cross area field values against the specified data field.
GetCrossAreaKey(Object[])In OLAP and server mode, returns cross area values by which you want to sort the pivot grid column or row.
SetArgs(IQueryMemberProvider, IQueryMemberProvider, IPivotGridField, PivotGridData, ICustomSortHelper)

Remarks

Note

The PivotGridField.SortMode property of the sorted field should be set to Custom to apply custom sorting. Otherwise, the CustomServerModeSort event will not be raised.

Example: Implement a Custom Sorting Algorithm

This example demonstrates how to implement a custom sorting algorithm by handling the CustomServerModeSort event. The “Month” column field is sorted by the “Produce” row using the cross area key, and the “Confections” field is sorted directly by the 1998 year.

csharp
using System.Windows;
using DevExpress.Xpf.PivotGrid;
using System.Collections;

namespace WPFPivotGridCustomServerModeSort {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            PivotGridField field = fieldOrderYear;
            pivotGridControl1.BeginUpdate();
            try {
                field.FilterValues.Clear();
                field.FilterValues.Add(1998);
                field.FilterValues.FilterType = FieldFilterType.Included;
            }
            finally {
                pivotGridControl1.EndUpdate();
            }
            fieldCategoryName.SortMode = FieldSortMode.Custom;
            fieldOrderMonth.SortMode = FieldSortMode.Custom;
        }

        private void pivotGridControl1_CustomServerModeSort(object sender, 
            CustomServerModeSortEventArgs e) {
            // Sorting using a cross area object.
            if (e.Field == fieldOrderMonth) {
                // Sets the cross area key, by which the "Month" field will be sorted. 
                // In this example, it's one of the "Category" cross area field values.
                CrossAreaKey sorting = e.GetCrossAreaKey(new object[] { "Confections" });

                // Sets the result of the "Month" field's values comparison 
                // by the cross area key object and the "Price" field.
                e.Result = Comparer.Default.Compare(
                    e.GetCellValue1(sorting, fieldPrice),
                    e.GetCellValue2(sorting, fieldPrice)
                );
            }

            // Direct sorting without using a cross area object. 
            if (e.Field == fieldCategoryName) {
                // Sets the result of "Category" field's values comparison by the Year and Price fields.
                e.Result = Comparer.Default.Compare(
                    e.GetCellValue1(new object[] { 1998 }, fieldPrice),
                    e.GetCellValue2(new object[] { 1998 }, fieldPrice)
                );
            }
        }
    }
}
vb
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid
Imports System.Collections

Namespace WPFPivotGridCustomServerModeSort
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
            Dim field As PivotGridField = fieldOrderYear
            pivotGridControl1.BeginUpdate()
            Try
                field.FilterValues.Clear()
                field.FilterValues.Add(1998)
                field.FilterValues.FilterType = FieldFilterType.Included
            Finally
                pivotGridControl1.EndUpdate()
            End Try
            fieldCategoryName.SortMode = FieldSortMode.Custom
            fieldOrderMonth.SortMode = FieldSortMode.Custom
        End Sub

        Private Sub pivotGridControl1_CustomServerModeSort(ByVal sender As Object,
                                    ByVal e As CustomServerModeSortEventArgs)
            ' Sorting using a cross area object.
            If e.Field Is fieldOrderMonth Then
                ' Sets the cross area key, by which the "Month" field will be sorted.
                ' In this example, it's one of the "Category" cross area field values.
                Dim sorting As CrossAreaKey = e.GetCrossAreaKey(New Object() {"Confections"})

                ' Sets the result of the "Month" field's values comparison 
                ' by the cross area key object and the "Price" field.
                e.Result = Comparer.Default.Compare(e.GetCellValue1(sorting, fieldPrice),
                                                    e.GetCellValue2(sorting, fieldPrice))
            End If

            ' Direct sorting without using a cross area object. 
            If e.Field Is fieldCategoryName Then
                ' Sets the result of "Category" field's values comparison by the Year and Price fields.
                e.Result = Comparer.Default.Compare(e.GetCellValue1(New Object() {1998}, fieldPrice),
                                                    e.GetCellValue2(New Object() {1998}, fieldPrice))
            End If
        End Sub
    End Class
End Namespace

See Also

PivotGridControl Class

PivotGridControl Members

DevExpress.Xpf.PivotGrid Namespace