ASPxPivotGrid.CustomCellValue Event

Allows you to replace cell values with custom ones.

Namespace: DevExpress.Web.ASPxPivotGrid

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

Declaration

public event EventHandler<PivotCellValueEventArgs> CustomCellValue
Public Event CustomCellValue As EventHandler(Of PivotCellValueEventArgs)

Event Data

The CustomCellValue event handler receives an argument of the PivotCellValueEventArgs type. The following properties provide information specific to this event.

Property Description
CellItem For internal use.
(Inherited from PivotCellBaseEventArgs)
ColumnCustomTotal Gets the column custom total which displays the current cell.
ColumnField Gets the innermost column field which corresponds to the processed cell.
ColumnFieldIndex For internal use.
ColumnIndex Gets the visual index of the column that contains the processed cell.
ColumnValueType Gets the type of column which contains the processed cell.
Data For internal use.
DataField Gets the data field which identifies the column where the processed cell resides.
Item For internal use.
RowCustomTotal Gets the row custom total which contains the current cell.
RowField Gets the innermost row field which corresponds to the processed cell.
RowFieldIndex For internal use.
RowIndex Gets the index of the row that contains the processed cell.
RowValueType Gets the type of row which contains the processed cell.
SummaryType Gets the summary type of the currently processed value.
SummaryValue Gets the summary value currently being processed.
Value Gets or sets the processed cell's value.

Remarks

The CustomCellValue event fires for each cell. You can handle it to replace the calculated cell values with custom ones. Use the event parameter's PivotCellEventArgsBase<TField, TData, TCustomTotal>.SummaryValue property to obtain the summary value calculated for the cell. The custom cell value should be provided to the PivotCellEventArgsBase<TField, TData, TCustomTotal>.Value property.

Use the event parameter's PivotCellEventArgsBase<TField, TData, TCustomTotal>.ColumnField, PivotCellEventArgsBase<TField, TData, TCustomTotal>.DataField and PivotCellEventArgsBase<TField, TData, TCustomTotal>.RowField properties to obtain the column, data and row fields that correspond to the processed cell, respectively. The PivotCellEventArgsBase<TField, TData, TCustomTotal>.ColumnIndex and PivotCellEventArgsBase<TField, TData, TCustomTotal>.RowIndex properties are used to obtain the visual indices of the column and row that contain the cell.

You can also provide custom display text by handling the ASPxPivotGrid.CustomCellDisplayText event, which is raised after the CustomCellValue event.

Note

Values provided with the CustomCellValue event cannot be used to sort data according to the PivotGridFieldBase.SortBySummaryInfo object's settings.

Examples

This example shows how to calculate a percent of a current cell related to the first row value, regardless of the current sorting.

To accomplish this task, handle the ASPxPivotGrid.CustomCellValue event. To obtain a cell value, address it. Create arrays of row and column values corresponding to the desired cell, and call the PivotCellEventArgsBase<TField, TData, TCustomTotal>.GetCellValue method, passing them in arguments.

The image below shows the result.

ExCustomCellValue

using System;
using DevExpress.XtraPivotGrid;
using DevExpress.Utils;

namespace WebAppCustomCellValue
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        object beveragesValue = "Beverages";
        DevExpress.Web.ASPxPivotGrid.PivotGridField fieldPercentOfBeverages;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                // Creates and specifies a 'Percent' field.
                fieldPercentOfBeverages = ASPxPivotGrid1.Fields.Add("Extended Price",
                    PivotArea.DataArea);
                fieldPercentOfBeverages.Caption = "Percent";
                fieldPercentOfBeverages.CellFormat.FormatType = FormatType.Numeric;
                fieldPercentOfBeverages.CellFormat.FormatString = "P";
                fieldPercentOfBeverages.ID = "fieldPercent";
            }
        }

        protected void ASPxPivotGrid1_CustomCellValue(object sender,
            DevExpress.Web.ASPxPivotGrid.PivotCellValueEventArgs e)
        {
            // Calculates 'Percent' field values.
            if (e.DataField.ID == "fieldPercent")
            {
                object[] columnPath = GetValues(e, true,
                    ASPxPivotGrid1.Fields.GetFieldByName("fieldCategoryName"), "Beverages");
                object[] rowPath = GetValues(e, false,
                    ASPxPivotGrid1.Fields.GetFieldByName("fieldCategoryName"), "Beverages");
                decimal beveragesValue =
                    Convert.ToDecimal(e.GetCellValue(columnPath, rowPath, e.DataField));
                if (beveragesValue == 0)
                    e.Value = null;
                else
                    e.Value = Convert.ToDecimal(e.Value) / beveragesValue;
            }
            else return;
        }
        object[] GetValues(DevExpress.Web.ASPxPivotGrid.PivotCellValueEventArgs e, bool isColumn,
            PivotGridFieldBase targetField, object targetValue)
        {
            DevExpress.Web.ASPxPivotGrid.PivotGridField[] fields = 
                isColumn ? e.GetColumnFields() : e.GetRowFields();
            object[] targetPath = new object[fields.Length];
            for (int i = 0; i < targetPath.Length; i++)
            {
                if (object.ReferenceEquals(fields[i], targetField))
                    targetPath[i] = targetValue;
                else
                    targetPath[i] = e.GetFieldValue(fields[i]);
            }
            return targetPath;
        }
    }
}
See Also