Back to Devexpress

ExpressionDataBinding Class

windowsforms-devexpress-dot-xtrapivotgrid-e5d49d04.md

latest6.6 KB
Original Source

ExpressionDataBinding Class

Defines a calculation based on a string expression.

Namespace : DevExpress.XtraPivotGrid

Assembly : DevExpress.XtraPivotGrid.v25.2.dll

NuGet Package : DevExpress.Win.PivotGrid

Declaration

csharp
public class ExpressionDataBinding :
    ExpressionBindingBase
vb
Public Class ExpressionDataBinding
    Inherits ExpressionBindingBase

Remarks

Pivot Grid allows you to create calculated fields. They do not obtain their values from fields in the data source. Instead, you specify a binding expression. The expression can be a formula or an aggregate function.

Note

Use OLAPExpressionBinding for OLAP mode.

Follow the steps below to create a calculated field in Optimized and Server modes:

  1. Create an ExpressionDataBinding instance and pass the expression in its constructor as a parameter.
  2. Assign the created object to the PivotGridFieldBase.DataBinding property.

The following code snippet shows how to bind fieldSalesPerson to the expression:

csharp
PivotGridFieldBase fieldSalesPerson = pivotGridControl1.Fields.Add();
fieldSalesPerson.DataBinding = new ExpressionDataBinding(string.Format(
    "Concat([{0}], ' ', [{1}], ' (', [{2}], ')')", fieldFirstName.Name, fieldLastName.Name, fieldEmployeeID.Name));
vb
Dim fieldSalesPerson As PivotGridFieldBase = pivotGridControl1.Fields.Add()
fieldSalesPerson.DataBinding = New ExpressionDataBinding(String.Format(
    "Concat([{0}], ' ', [{1}], ' (', [{2}], ')')", fieldFirstName.Name, fieldLastName.Name, fieldEmployeeID.Name))

Run Demo: Calculated Fields

Refer to the following article for more information: Calculated Fields.

You can create functions with custom logic to build an expression that executes complex calculations for a Pivot Grid’s field.

Refer to the following articles for more information about custom functions:

The following example specifies a custom summary for the First Product Sold field. The custom summary’s expression (FirstValue([ProductName])) uses a custom aggregate function (FirstValue) to return the first product sold by a sales person in each product category.

View Example

cs
using DevExpress.XtraPivotGrid;
using System.Windows.Forms;

namespace WinPivot_CustomFunctions {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();     
            // ...
            pivotGridControl1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized;
            PivotGridField pivotGridField1 = new PivotGridField() {
                Area = PivotArea.DataArea,
                AreaIndex = 0,
                Caption = "First Product Sold",
                FieldName = "FirstProductSold"
            };
            pivotGridControl1.Fields.Add(pivotGridField1);
            pivotGridField1.DataBinding = new ExpressionDataBinding() { 
                Expression = "FirstValue([ProductName])" };
            pivotGridField1.Options.ShowExpressionEditorMenu = true;
            pivotGridField1.Options.ShowGrandTotal = false; 
        }      
    }
}
vb
Imports System.Windows.Forms
Imports DevExpress.XtraPivotGrid

Namespace WinPivot_CustomFunctions
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
            ' ...
            pivotGridControl1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized
            Dim pivotGridField1 As New PivotGridField() With {
                .Area = PivotArea.DataArea,
                .AreaIndex = 0,
                .Caption = "First Product Sold",
                .FieldName = "FirstProductSold"
            }
            pivotGridControl1.Fields.Add(pivotGridField1)
            pivotGridField1.DataBinding = New ExpressionDataBinding() With {.Expression = "FirstValue([ProductName])"}
            pivotGridField1.Options.ShowExpressionEditorMenu = True
            pivotGridField1.Options.ShowGrandTotal = False
        End Sub
    End Class
End Namespace

More Examples

View Example: Pivot Grid for WinForms - Display KPI Graphics

Inheritance

Object DataBindingBase ExpressionBindingBase ExpressionDataBinding

See Also

ExpressionDataBinding Members

Bind Pivot Grid Fields to Calculated Expressions

Pivot Grid Data Processing Modes

Bind Pivot Grid Fields to Data Columns

Bind Pivot Grid Fields to Calculated Expressions

Bind Pivot Grid Fields to Window Calculations

DevExpress.XtraPivotGrid Namespace