The following example demonstrates how to implement Sorting by Summary in OLAP mode.
In this example, values of the Semester field are sorted by the Australia | Bendigo column summary values. To do this, two sort conditions represented by SortByCondition instances are created. One of them represents an OLAP member that corresponds to the ‘Australia’ value, while another corresponds to the ‘Bendigo’ value. These sort conditions are added to the Semester field’s PivotGridField.SortByConditions collection to specify the column by which Semester values should be sorted. A data field that identifies the column is specified via the PivotGridField.SortByField property.
OLAP members corresponding to the Australia and Bendigo values are obtained using the PivotGridControl.GetFieldValueOlapMember method. Note that OLAP members can be obtained only for visible field values. For this reason, the Australia field value is expanded before initializing OLAP members in order to obtain the Bendigo OLAP member.
This sample uses the Adventure Works 2008 cube.
<Window xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="DXPivotGrid_OLAPSortBySummary.MainWindow"
Height="600" Width="800" Title="Main Window">
<Grid>
<dxpg:PivotGridControl Name="pivotGridControl1" Loaded="pivotGridControl1_Loaded">
<dxpg:PivotGridControl.Fields>
<dxpg:PivotGridField Name="fieldMonth" Area="RowArea" Caption="Month"
FieldName="[Date].[Month of Year].[Month of Year]"/>
<dxpg:PivotGridField Name="fieldCountry" Area="ColumnArea" Caption="Country"
FieldName="[Customer].[Country].[Country]"/>
<dxpg:PivotGridField Name="fieldSales" Area="DataArea" Caption="Sales"
FieldName="[Measures].[Internet Sales Amount]"/>
<dxpg:PivotGridField Name="fieldCity" Area="ColumnArea" Caption="City"
FieldName="[Customer].[City].[City]"/>
</dxpg:PivotGridControl.Fields>
</dxpg:PivotGridControl>
</Grid>
</Window>
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid
Namespace DXPivotGrid_OLAPSortBySummary
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
pivotGridControl1.OlapConnectionString = "provider=MSOLAP;" &
"data source=http://demos.devexpress.com/Services/OLAP/msmdpump.dll;" &
"initial catalog='Adventure Works DW Standard Edition';" &
"cube name='Adventure Works'"
End Sub
Private Sub pivotGridControl1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Expands the Australia column to be able to retrieve OLAP members
' that correspond to the nested columns.
pivotGridControl1.ExpandValue(True, New Object() { "Australia" })
' Obtains OLAP members corresponding to the Australia and Bendigo values.
Dim countryMember As PivotOlapMember = pivotGridControl1.GetFieldValueOlapMember( _
fieldCountry, 0)
Dim cityMember As PivotOlapMember = pivotGridControl1.GetFieldValueOlapMember( _
fieldCity, 0)
' Exits if the OLAP members were not obtained successfully.
If countryMember Is Nothing OrElse cityMember Is Nothing Then
Return
End If
' Locks the pivot grid from updating while the Sort by Summary
' settings are being customized.
pivotGridControl1.BeginUpdate()
Try
' Specifies a data field whose summary values should be used to sort values
' of the Month field.
fieldMonth.SortByField = fieldSales
' Specifies a column by which the Month field values should be sorted.
fieldMonth.SortByConditions.Add(New SortByCondition(fieldCountry, _
"Australia", countryMember.UniqueName))
fieldMonth.SortByConditions.Add(New SortByCondition(fieldCity, _
"Bendigo", cityMember.UniqueName))
Finally
' Unlocks the pivot grid and applies changes.
pivotGridControl1.EndUpdate()
End Try
End Sub
End Class
End Namespace
using System.Windows;
using DevExpress.Xpf.PivotGrid;
namespace DXPivotGrid_OLAPSortBySummary {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
pivotGridControl1.OlapConnectionString = "provider=MSOLAP;" +
"data source=http://demos.devexpress.com/Services/OLAP/msmdpump.dll;" +
"initial catalog='Adventure Works DW Standard Edition';" +
"cube name='Adventure Works'";
}
private void pivotGridControl1_Loaded(object sender, RoutedEventArgs e) {
// Expands the Australia column to be able to retrieve OLAP members
// that correspond to the nested columns.
pivotGridControl1.ExpandValue(true, new object[] { "Australia" });
// Obtains OLAP members corresponding to the Australia and Bendigo values.
PivotOlapMember countryMember = pivotGridControl1.GetFieldValueOlapMember(fieldCountry, 0);
PivotOlapMember cityMember = pivotGridControl1.GetFieldValueOlapMember(fieldCity, 0);
// Exits if the OLAP members were not obtained successfully.
if (countryMember == null || cityMember == null)
return;
// Locks the pivot grid from updating while the Sort by Summary
// settings are being customized.
pivotGridControl1.BeginUpdate();
try {
// Specifies a data field whose summary values should be used to sort values
// of the Month field.
fieldMonth.SortByField = fieldSales;
// Specifies a column by which the Month field values should be sorted.
fieldMonth.SortByConditions.Add(
new SortByCondition(fieldCountry, "Australia", countryMember.UniqueName));
fieldMonth.SortByConditions.Add(
new SortByCondition(fieldCity, "Bendigo", cityMember.UniqueName));
}
finally {
// Unlocks the pivot grid and applies changes.
pivotGridControl1.EndUpdate();
}
}
}
}