Back to Devexpress

FieldSortLocation Enum

wpf-devexpress-dot-xpf-dot-pivotgrid-ba7b0f3c.md

latest10.8 KB
Original Source

FieldSortLocation Enum

Lists values that specify a target UI element whose items are sorted.

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public enum FieldSortLocation
vb
Public Enum FieldSortLocation

Members

NameDescription
Pivot

Specifies the Pivot Grid’s data.

| | Filter |

Specifies items in a filter drop-down window.

| | GroupFilter |

Specifies the group filter items.

|

The following properties accept/return FieldSortLocation values:

Remarks

Use the PivotCustomFieldSortEventArgs.SortLocation property to get the target UI element.

Example

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.

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 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.

xaml
<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>
csharp
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.PivotGrid;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace Wpf_PivotGrid_CustomFieldSort_Example
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<MyOrderRow> OrderSourceList {get; set;}
        public MainWindow()
        {
            InitializeComponent();
            pivotGridControl1.CustomFieldSort += PivotGridControl1_CustomFieldSort;
            checkEdit1.Checked += CheckEdit1_Checked;
            checkEdit1.Unchecked += CheckEdit1_Unchecked;
        }

        private void CheckEdit1_Unchecked(object sender, RoutedEventArgs e)
        {
            // Set the default sorting algorithm.
            fieldSalesPerson.SortMode = FieldSortMode.Default;
        }

        private void CheckEdit1_Checked(object sender, RoutedEventArgs e)
        {
            // Enable the CustomFieldSort event for the fieldSalesPerson field.
            fieldSalesPerson.SortMode = FieldSortMode.Custom;
        }

        private void PivotGridControl1_CustomFieldSort(object sender, DevExpress.Xpf.PivotGrid.PivotCustomFieldSortEventArgs e)
        {
            if (e.Field.FieldName == "SalesPersonName")
            {
                int result;
                if (e.SortLocation == FieldSortLocation.Pivot)
                {
                    object orderValue1 = e.GetListSourceColumnValue(e.ListSourceRowIndex1, "SalesPersonId"),
                        orderValue2 = e.GetListSourceColumnValue(e.ListSourceRowIndex2, "SalesPersonId");
                    result = Comparer.Default.Compare(orderValue1, orderValue2);
                }
                else
                {
                    // Compare last names.
                    result = Comparer.Default.Compare(e.Value1.ToString().Split(' ')[1], e.Value2.ToString().Split(' ')[1]);
                    // If last names are the same, compare first names.
                    if (result == 0)
                        result = Comparer.Default.Compare(e.Value1.ToString().Split(' ')[0], e.Value2.ToString().Split(' ')[0]);
                }
                e.Result = result;
                e.Handled = true;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            OrderSourceList = DatabaseHelper.CreateData();
            pivotGridControl1.DataSource = OrderSourceList;
            pivotGridControl1.BestFitArea = DevExpress.Xpf.PivotGrid.FieldBestFitArea.FieldHeader;
            pivotGridControl1.BestFit();
        }
    }
}
vb
Imports DevExpress.Xpf.Editors
Imports DevExpress.Xpf.PivotGrid
Imports System
Imports System.Collections
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Windows

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

        Public Property OrderSourceList() As ObservableCollection(Of MyOrderRow)
        Public Sub New()
            InitializeComponent()
            AddHandler pivotGridControl1.CustomFieldSort, AddressOf PivotGridControl1_CustomFieldSort
            AddHandler checkEdit1.Checked, AddressOf CheckEdit1_Checked
            AddHandler checkEdit1.Unchecked, AddressOf CheckEdit1_Unchecked
        End Sub

        Private Sub CheckEdit1_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Set the default sorting algorithm.
            fieldSalesPerson.SortMode = FieldSortMode.Default
        End Sub

        Private Sub CheckEdit1_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Enable the CustomFieldSort event for the fieldSalesPerson field.
            fieldSalesPerson.SortMode = FieldSortMode.Custom
        End Sub

        Private Sub PivotGridControl1_CustomFieldSort(ByVal sender As Object, ByVal e As DevExpress.Xpf.PivotGrid.PivotCustomFieldSortEventArgs)
            If e.Field.FieldName = "SalesPersonName" Then
                Dim result As Integer
                If e.SortLocation = FieldSortLocation.Pivot Then
                    Dim orderValue1 As Object = e.GetListSourceColumnValue(e.ListSourceRowIndex1, "SalesPersonId"), orderValue2 As Object = e.GetListSourceColumnValue(e.ListSourceRowIndex2, "SalesPersonId")
                    result = Comparer.Default.Compare(orderValue1, orderValue2)
                Else
                    ' Compare last names.
                    result = Comparer.Default.Compare(e.Value1.ToString().Split(" "c)(1), e.Value2.ToString().Split(" "c)(1))
                    ' If last names are the same, compare first names.
                    If result = 0 Then
                        result = Comparer.Default.Compare(e.Value1.ToString().Split(" "c)(0), e.Value2.ToString().Split(" "c)(0))
                    End If
                End If
                e.Result = result
                e.Handled = True
            End If
        End Sub

        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            OrderSourceList = DatabaseHelper.CreateData()
            pivotGridControl1.DataSource = OrderSourceList
            pivotGridControl1.BestFitArea = DevExpress.Xpf.PivotGrid.FieldBestFitArea.FieldHeader
            pivotGridControl1.BestFit()
        End Sub
    End Class
End Namespace

See Also

DevExpress.Xpf.PivotGrid Namespace