How to: Use the CustomFieldSort Event and a Hidden Field to Sort the Visible Field

  • 4 minutes to read

This example demonstrates how to sort the Sales Person field values:

  • in the PivotGridControl by the hidden data field (SalesPersonId)
  • in the Filter Drop-Down by the person's last name

Check the Enable custom sorting box to apply a custom sorting algorithm instead of the default alphabetical sorting order. In the picture below, the SalesPersonId value is appended to the name displayed in the Sales Person field for better visibility.

screenshot

The checked Enable custom sorting box switches the PivotGridField.SortMode property to the FieldSortMode.Custom value. This setting instructs the PivotGridControl to fire the [PivotGridControl.CustomFieldSort]xref:DevExpress.Xpf.PivotGrid.PivotGridControl.CustomFieldSort) event for that field.

The CustomFieldSort handler uses the e.SortLocation property to determine whether the field values are displayed in the pivot grid, or in the filter popup.

If the field is displayed in the pivot grid, the code compares values obtained from the SalesPersonId column and assigns the result to the e.Result property.

If the field is displayed in the filter popup, the e.ListSourceRowIndex1 and e.ListSourceRowIndex2 properties are always -1 and cannot be used to determine the underlying data row. In this situation, the comparison algorithm processes the field value itself.

NOTE

The complete sample project How to Use the CustomFieldSort Event and a Hidden Field to Sort the Visible Field is available in the DevExpress Examples repository.

<Window
    x:Class="Wpf_PivotGrid_CustomFieldSort_Example.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
    xmlns:local="clr-namespace:Wpf_PivotGrid_CustomFieldSort_Example"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="800"
    Height="450"
    mc:Ignorable="d"
    Loaded="Window_Loaded"
    Title="CustomFieldSort Example">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <dxpg:PivotGridControl x:Name="pivotGridControl1" Grid.Row="1">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField
                    x:Name="fieldSales"
                    Area="DataArea"
                    Caption="Product Sales"
                    FieldName="ExtendedPrice" />
                <dxpg:PivotGridField
                    x:Name="fieldSalesPerson"
                    Area="ColumnArea"
                    Caption="Sales Person"
                    FieldName="SalesPersonName" />
                <dxpg:PivotGridField
                    x:Name="fieldCategoryName"
                    Area="RowArea"
                    AreaIndex="0"
                    Caption="Category Name"
                    FieldName="CategoryName" />
                <dxpg:PivotGridField
                    x:Name="fieldProductName"
                    Area="RowArea"
                    AreaIndex="1"
                    Caption="Product Name"
                    FieldName="ProductName" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
        <dxe:CheckEdit
            x:Name="checkEdit1"
            Width="150"
            Margin="10,10,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Content="Enable custom sorting" />
    </Grid>

</Window>