Back to Devexpress

ASPxPivotGrid.HtmlFieldValuePrepared Event

aspnet-devexpress-dot-web-dot-aspxpivotgrid-dot-aspxpivotgrid-53848b0a.md

latest17.6 KB
Original Source

ASPxPivotGrid.HtmlFieldValuePrepared Event

Enables the settings of individual field value cells to be changed.

Namespace : DevExpress.Web.ASPxPivotGrid

Assembly : DevExpress.Web.ASPxPivotGrid.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event PivotHtmlFieldValuePreparedEventHandler HtmlFieldValuePrepared
vb
Public Event HtmlFieldValuePrepared As PivotHtmlFieldValuePreparedEventHandler

Event Data

The HtmlFieldValuePrepared event's data class is PivotHtmlFieldValuePreparedEventArgs. The following properties provide information specific to this event:

PropertyDescription
CellGets the processed data cell.
CustomTotalGets the custom total that corresponds to the currently processed column/row header. Inherited from PivotFieldValueEventArgsBase<T>.
DataFor internal use. Inherited from PivotFieldValueEventArgs.
DataFieldGets the data field that specifies the processed value. Inherited from PivotFieldValueEventArgsBase<T>.
FieldGets the field being processed. Inherited from PivotFieldEventArgsBase<T>.
FieldIndexGets the field position among the visible fields within the header area. Inherited from PivotFieldValueEventArgsBase<T>.
IsCollapsedGets whether the processed field value is collapsed. Inherited from PivotFieldValueEventArgsBase<T>.
IsColumnGets whether the field is displayed within the Column Header Area. Inherited from PivotFieldValueEventArgsBase<T>.
IsOthersValueGets or sets whether the current header corresponds to the “Others” row/column. Inherited from PivotFieldValueEventArgsBase<T>.
ItemFor internal use. Inherited from PivotFieldValueEventArgsBase<T>.
MaxIndexGets the maximum row index (for row fields) or column index (for column fields) that corresponds to the processed field value. Inherited from PivotFieldValueEventArgsBase<T>.
MinIndexGets the minimum row index (for row fields) or column index (for column fields) that corresponds to the processed field value. Inherited from PivotFieldValueEventArgsBase<T>.
ValueGets the column field or row field value that corresponds to the currently processed column/row header. Inherited from PivotFieldValueEventArgsBase<T>.
ValueTypeGets the type of the currently processed header of a column or a row. Inherited from PivotFieldValueEventArgsBase<T>.

The event data class exposes the following methods:

MethodDescription
ChangeExpandedState()Changes the expanded state of the field value currently being processed. Inherited from PivotFieldValueEventArgsBase<T>.
CreateDrillDownDataSource()Returns data records that are used to calculate a summary value for the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
CreateDrillDownDataSource(List<String>)Returns data records that are used to calculate a summary value for the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
CreateDrillDownDataSource(Int32, List<String>)Returns data records used to calculate a summary value for the specified cell in OLAP and server mode. Inherited from PivotFieldValueEventArgsBase<T>.
CreateDrillDownDataSource(Int32)Returns data records that are used to calculate a summary value for the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
CreateOLAPDrillDownDataSource(Int32, List<String>)Obsolete. In OLAP mode, returns a list of records used to calculate a summary value for the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
CreateServerModeDrillDownDataSource(Int32, List<String>)Obsolete. In server mode, returns a list of records used to calculate a summary value for the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
GetCellValue(Int32, Int32)Returns a value displayed in the specified cell. Inherited from PivotFieldValueEventArgsBase<T>.
GetFieldValue(T, Int32)Returns the specified column or row field value for the cell, addressed by its zero-based index in the Data Area. Inherited from PivotFieldValueEventArgsBase<T>.
GetHigherLevelFields()Returns the parent field for the field value currently being processed. Inherited from PivotFieldValueEventArgsBase<T>.
GetHigherLevelFieldValue(T)Returns the value of a specific parent field corresponding to the field value currently being processed. Inherited from PivotFieldValueEventArgsBase<T>.

Remarks

The HtmlFieldValuePrepared event is raised for each field value cell within the ASPxPivotGrid when the corresponding table cell has been created. You can handle this event to change the style settings of individual cells.

The processed cell is identified by the event parameter’s PivotHtmlFieldValuePreparedEventArgs.Cell property.

Example

This example demonstrates how to use the ASPxPivotGrid.HtmlFieldValuePrepared event to customize the appearance of a specific Field Value.

  • If the Country field value is equal to ‘Brazil’, the background color is set to LightGreen.
  • If the Country field value is equal to ‘Argentina’, the background color is set to LightBlue.
  • In other cases, the background color of the Country field values is set to LightYellow.

The image below shows the result.

aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
           Inherits="FormatCellValues._Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v __, Version=__ ,
           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" Theme="Metropolis"
            onhtmlfieldvalueprepared="ASPxPivotGrid1_HtmlFieldValuePrepared">
            <Fields>
                <dx:PivotGridField ID="fieldCountry" Area="RowArea"
                    AreaIndex="0" FieldName="Country">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldYear" Area="ColumnArea"
                    AreaIndex="0" FieldName="Year">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldTotal" Area="DataArea"
                    AreaIndex="0" FieldName="Total">
                </dx:PivotGridField>
            </Fields>
        </dx:ASPxPivotGrid>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/nwind.mdb" 
            SelectCommand="SELECT Customers.Country, Year([OrderDate]) AS [Year], Sum([UnitPrice]*[Quantity]) AS Total
FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
GROUP BY Customers.Country, Year([OrderDate])
HAVING (((Customers.Country) In ('Brazil','Argentina','Germany','USA', 'UK')));
">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
csharp
using System;
using System.Web.UI;
using System.Drawing;

namespace FormatCellValues {
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        }

        protected void ASPxPivotGrid1_HtmlFieldValuePrepared(object sender, DevExpress.Web.ASPxPivotGrid.PivotHtmlFieldValuePreparedEventArgs e) {
            if(Object.ReferenceEquals( e.Field, fieldCountry))
            {
                if (e.Value.ToString() == "Brazil")
                    e.Cell.Style[HtmlTextWriterStyle.BackgroundColor] = ColorTranslator.ToHtml( Color.LightGreen );
                else if (e.Value.ToString() == "Argentina")
                    e.Cell.Style[HtmlTextWriterStyle.BackgroundColor] = ColorTranslator.ToHtml( Color.LightBlue);
                else
                    e.Cell.Style[HtmlTextWriterStyle.BackgroundColor] = ColorTranslator.ToHtml( Color.LightYellow );
            }
        }
    }
}
vb
Imports System
Imports System.Web.UI
Imports System.Drawing

Namespace FormatCellValues
    Partial Public Class _Default
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub

        Protected Sub ASPxPivotGrid1_HtmlFieldValuePrepared(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxPivotGrid.PivotHtmlFieldValuePreparedEventArgs)
            If Object.ReferenceEquals(e.Field, fieldCountry) Then
                If e.Value.ToString() = "Brazil" Then
                    e.Cell.Style(HtmlTextWriterStyle.BackgroundColor) = ColorTranslator.ToHtml(Color.LightGreen)
                ElseIf e.Value.ToString() = "Argentina" Then
                    e.Cell.Style(HtmlTextWriterStyle.BackgroundColor) = ColorTranslator.ToHtml(Color.LightBlue)
                Else
                    e.Cell.Style(HtmlTextWriterStyle.BackgroundColor) = ColorTranslator.ToHtml(Color.LightYellow)
                End If
            End If
        End Sub
    End Class
End Namespace
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb"
           Inherits="FormatCellValues._Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v __, Version=__ ,
           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" Theme="Metropolis"
            onhtmlfieldvalueprepared="ASPxPivotGrid1_HtmlFieldValuePrepared">
            <Fields>
                <dx:PivotGridField ID="fieldCountry" Area="RowArea"
                    AreaIndex="0" FieldName="Country">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldYear" Area="ColumnArea"
                    AreaIndex="0" FieldName="Year">
                </dx:PivotGridField>
                <dx:PivotGridField ID="fieldTotal" Area="DataArea"
                    AreaIndex="0" FieldName="Total">
                </dx:PivotGridField>
            </Fields>
        </dx:ASPxPivotGrid>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/nwind.mdb" 
            SelectCommand="SELECT Customers.Country, Year([OrderDate]) AS [Year], Sum([UnitPrice]*[Quantity]) AS Total
FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
GROUP BY Customers.Country, Year([OrderDate])
HAVING (((Customers.Country) In ('Brazil','Argentina','Germany','USA', 'UK')));
">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>

See Also

HtmlCellPrepared

ASPxPivotGrid Class

ASPxPivotGrid Members

DevExpress.Web.ASPxPivotGrid Namespace