Back to Devexpress

Custom Formatting

aspnet-7293-components-pivot-grid-data-formatting-custom-formatting.md

latest7.2 KB
Original Source

Custom Formatting

  • Dec 17, 2020
  • 3 minutes to read

The Pivot Grid allows custom text to be displayed within individual data cells. To do this, handle the ASPxPivotGrid.CustomCellDisplayText event. This event is fired for each cell in bound and unbound fields.

Each data cell displays a summary value calculated against Data Field. This data field is returned by the event parameter’s PivotCellEventArgsBase<TField, TData, TCustomTotal>.DataField property. The cell’s current value is returned by the event parameter’s PivotCellEventArgsBase<TField, TData, TCustomTotal>.Value property.

To change the cell’s display text, use the PivotCellDisplayTextEventArgs.DisplayText property. Initially, this property contains the text currently displayed within a cell. To provide a custom text, assign it to the PivotCellDisplayTextEventArgs.DisplayText property.

Example: How to Format Cells via Events

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

In this example, if a Grand Total value is less than 50 000, ASPxPivotGrid displays the Low value instead. If the value exceeds 100 000, High is displayed; otherwise, Middle.

csharp
using System;
using System.Web.UI;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;

namespace FormattingViaEvents {
    public partial class _Default : Page {
        protected void Page_Load(object sender, EventArgs e) {
        }
        protected void CustomCellDisplayText(object sender, PivotCellDisplayTextEventArgs e) {
            if (e.RowValueType != PivotGridValueType.GrandTotal ||
                e.ColumnValueType == PivotGridValueType.GrandTotal) return;
            if (Convert.ToSingle(e.Value) < 50000)
                e.DisplayText = "Low";
            else if (Convert.ToSingle(e.Value) > 100000)
                e.DisplayText = "High";
            else
                e.DisplayText = "Middle";
        }
    }
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
           Inherits="FormattingViaEvents._Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v9.3, Version=9.3.1.0,
          Culture=neutral, PublicKeyToken=b88d1754d700e49a"
          Namespace="DevExpress.Web.ASPxPivotGrid"
          TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
            OnCustomCellDisplayText="CustomCellDisplayText">
            <Fields>
                <dx:PivotGridField ID="fieldProductName" Area="RowArea"
                       AreaIndex="0" FieldName="ProductName">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea"
                       AreaIndex="0" FieldName="ExtendedPrice">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldCountry" Area="ColumnArea"
                       AreaIndex="0" FieldName="Country">
                </dx:PivotGridField>
            </Fields>
        </dx:ASPxPivotGrid>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server"
            DataFile="~/App_Data/nwind.mdb"
            SelectCommand="SELECT [ProductName], [ExtendedPrice], [Country] FROM [Invoices]">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb"
          Inherits="FormattingViaEvents._Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v9.3, Version=9.3.1.0,
          Culture=neutral, PublicKeyToken=b88d1754d700e49a"
          Namespace="DevExpress.Web.ASPxPivotGrid"
          TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
            OnCustomCellDisplayText="CustomCellDisplayText">
            <Fields>
                <dx:PivotGridField ID="fieldProductName" Area="RowArea"
                       AreaIndex="0" FieldName="ProductName">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea"
                       AreaIndex="0" FieldName="ExtendedPrice">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldCountry" Area="ColumnArea"
                       AreaIndex="0" FieldName="Country">
                </dx:PivotGridField>
            </Fields>
        </dx:ASPxPivotGrid>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server"
            DataFile="~/App_Data/nwind.mdb"
            SelectCommand="SELECT [ProductName], [ExtendedPrice], [Country] FROM [Invoices]">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI
Imports DevExpress.Web.ASPxPivotGrid
Imports DevExpress.XtraPivotGrid

Namespace FormattingViaEvents
     Partial Public Class _Default
          Inherits Page
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
          End Sub
          Protected Sub CustomCellDisplayText(ByVal sender As Object, _
                    ByVal e As PivotCellDisplayTextEventArgs)
               If e.RowValueType <> PivotGridValueType.GrandTotal OrElse _
                         e.ColumnValueType = PivotGridValueType.GrandTotal Then
                    Return
               End If
               If Convert.ToSingle(e.Value) < 50000 Then
                    e.DisplayText = "Low"
               ElseIf Convert.ToSingle(e.Value) > 100000 Then
                    e.DisplayText = "High"
               Else
                    e.DisplayText = "Middle"
               End If
          End Sub
     End Class
End Namespace