Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

FieldSortLocation Enum

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

Namespace: DevExpress.Xpf.PivotGrid

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

NuGet Package: DevExpress.Wpf.PivotGrid

#Declaration

public enum FieldSortLocation

#Members

Name Description
Pivot

Specifies the Pivot Grid’s data.

Filter

Specifies items in a filter drop-down window.

GroupFilter

Specifies the group filter items.

#Related API Members

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.

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

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();
        }
    }
}
See Also