Back to Devexpress

How to: Format Cells to Show Custom Display Text

wpf-119003-controls-and-libraries-pivot-grid-examples-grouping-filtering-and-summarizing-how-to-format-cells-to-show-custom-display-text.md

latest3.0 KB
Original Source

How to: Format Cells to Show Custom Display Text

  • Aug 01, 2019
  • 2 minutes to read

The following example shows how to provide custom text for the PivotGridControl’s cells by handling the PivotGridControl.CustomCellDisplayText event.

In this example, if a row total value is less than 2000, PivotGridControl displays the ‘Low’ value instead. If the value exceeds 6000, ‘High’ is displayed; otherwise, ‘Middle’.

vb
Imports DevExpress.Xpf.PivotGrid

Class MainWindow
    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        fieldCountry.FilterValues.FilterType = FieldFilterType.Included
        fieldCountry.FilterValues.Add("UK")
    End Sub
    Private Sub PivotGridControl1_CustomCellDisplayText(sender As System.Object,
                e As DevExpress.Xpf.PivotGrid.PivotCellDisplayTextEventArgs) Handles pivotGridControl1.CustomCellDisplayText
        ' In this example, if a row total value is less than 2000, 'Low' is displayed instead. 
        ' If the value exceeds 6000, 'High' is displayed; otherwise, 'Middle'.
        If e.RowValueType = FieldValueType.Total OrElse e.ColumnValueType <> FieldValueType.Total Then
            Return
        End If
        If Convert.ToSingle(e.Value) < 2000 Then
            e.DisplayText = "Low"
        ElseIf Convert.ToSingle(e.Value) > 6000 Then
            e.DisplayText = "High"
        Else
            e.DisplayText = "Middle"
        End If
    End Sub
End Class
csharp
using System;
using System.Windows;
using DevExpress.Xpf.PivotGrid;

namespace WpfPivotCustomCellValueDisplaytext
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            fieldCountry.FilterValues.FilterType = FieldFilterType.Included;
            fieldCountry.FilterValues.Add("UK");
        }

        private void pivotGridControl1_CustomCellDisplayText(object sender, 
                        DevExpress.Xpf.PivotGrid.PivotCellDisplayTextEventArgs e)
        {
            // In this example, if a row total value is less than 2000, 'Low' is displayed instead. 
            // If the value exceeds 6000, 'High' is displayed; otherwise, 'Middle'.
            if (e.RowValueType == FieldValueType.Total ||
                e.ColumnValueType != FieldValueType.Total) return;
            if (Convert.ToSingle(e.Value) < 2000)
                e.DisplayText = "Low";
            else if (Convert.ToSingle(e.Value) > 6000)
                e.DisplayText = "High";
            else
                e.DisplayText = "Middle";
        }
    }
}