How to: Sort Data by OLAP Member Properties

  • 2 minutes to read

This example demonstrates how to sort data by OLAP member properties using the PivotGridControl.CustomServerModeSort event. In this example, the "Product" field's values are sorted by the "Color" OLAP member property.

using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
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;
        }

        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);
            }
        }
    }

}