Back to Devexpress

Bind Pivot Grid Fields to Calculated Expressions

wpf-404334-controls-and-libraries-pivot-grid-binding-to-data-olap-data-source-bind-pivot-grid-fields-to-calculated-expressions.md

latest3.3 KB
Original Source

Bind Pivot Grid Fields to Calculated Expressions

  • Aug 10, 2023
  • 2 minutes to read

This topic describes how to use the Binding API to create calculated fields in OLAP mode.

Calculated fields display the result of calculated expressions. Each calculated field has a binding expression that can be a formula or an aggregate function. The expression allows you to not only obtain values from a field in the data source, but specify exactly how to calculate the data (for example, aggregate it).

Note

You cannot use the Expression Editor dialog in OLAP mode.

Create a Calculated Field

Important

You cannot bind the Pivot Grid to data at design time in .NET 5+ projects.

OLAP mode supports OlapExpressionBinding.

Follow the steps below to create a calculated field in OLAP mode:

  1. Create an OLAPExpressionBinding instance and pass the expression in its constructor as a parameter. You can also use the object’s OLAPExpressionBindingBase.Expression property to specify the expression.
  2. Assign the created object to the PivotGridField.DataBinding property.

View Example: Pivot Grid for WPF - Bind a PivotGrid to an OLAP Cube

The following code snippet shows how to bind fieldSales to the MDX expression:

csharp
using System.Windows;
using DevExpress.Xpf.PivotGrid;

namespace HowToBindOLAP {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent(); 
         }
         private void Window_Loaded(object sender, RoutedEventArgs e) {
           // ...
            PivotGridField fieldSales = new PivotGridField();
            fieldSales.Caption = "Cleared Amount";
            fieldSales.Area = FieldArea.DataArea;
            fieldSales.DataBinding = new OlapExpressionBinding("[Measures].[Internet Sales Amount] * 0.87");
            fieldSales.CellFormat = "c";

            pivotGridControl1.Fields.Add(fieldSales); 
         }
    }
}
vb
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid

Namespace HowToBindOLAP
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
        End Sub
         Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
           ' ...
            Dim fieldSales As New PivotGridField()
            fieldSales.Caption = "Cleared Amount"
            fieldSales.Area = FieldArea.DataArea
            fieldSales.DataBinding = New OlapExpressionBinding("[Measures].[Internet Sales Amount] * 0.87")
            fieldSales.CellFormat = "c"

            pivotGridControl1.Fields.Add(fieldSales)
         End Sub
    End Class
End Namespace