Back to Devexpress

How to: Format Cells via Events

aspnet-7312-components-pivot-grid-examples-data-formatting-how-to-format-cells-via-events.md

latest5.8 KB
Original Source

How to: Format Cells via Events

  • Dec 17, 2020
  • 2 minutes to read

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