Skip to main content

PivotGridControl.CustomFieldValueCells Event

Allows you to customize field value cells.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.XtraPivotGrid.v24.2.dll

Declaration

public event PivotCustomFieldValueCellsEventHandler CustomFieldValueCells

Event Data

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

Property Description
ColumnCount Gets the number of columns in the pivot grid. Inherited from PivotCustomFieldValueCellsEventArgsBase.
IsUpdateRequired Gets whether the area where the field value cells reside needs to be redrawn after the event is handled. Inherited from PivotCustomFieldValueCellsEventArgsBase.
RowCount Gets the number of rows in the pivot grid. Inherited from PivotCustomFieldValueCellsEventArgsBase.

The event data class exposes the following methods:

Method Description
FindAllCells(Boolean, Predicate<Object[]>) Returns all cells whose values match the specified condition. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
FindCell(Boolean, Predicate<Object[]>) Returns the header of the column/row whose summary values match the specified condition. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
GetCell(Boolean, Int32) Returns the field value cell by its index. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
GetCellCount(Boolean) Returns the number of field value cells in the specified area. Inherited from PivotCustomFieldValueCellsEventArgsBase.
GetCellValue(Int32, Int32) Gets the value of a data cell by its column and row indexes. Inherited from PivotCustomFieldValueCellsEventArgsBase.
GetGrandTotalLocation(Boolean) Returns the location of Grand Total columns or rows. Inherited from PivotCustomFieldValueCellsEventArgsBase.
GetLevelCount(Boolean) Returns the number of column or row levels. Inherited from PivotCustomFieldValueCellsEventArgsBase.
Remove(FieldValueCellBase) Removes the specified field value cell. Inherited from PivotCustomFieldValueCellsEventArgsBase.
SetGrandTotalLocation(Boolean, GrandTotalLocation) Sets the location of Grand Total columns or rows to the specified value. Inherited from PivotCustomFieldValueCellsEventArgsBase.
Split(Boolean, Predicate<T2>, FieldValueSplitData[]) Splits all field value cells that match the specified condition. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
Split(Boolean, Predicate<T2>, Boolean, FieldValueSplitData[]) Splits all field value cells that match the specified condition, or only the first matching cell. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
Split(Boolean, Predicate<T2>, Boolean, IList<FieldValueSplitData>) Splits all field value cells that match the specified condition, or only the first matching cell. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.
Split(Boolean, Predicate<T2>, IList<FieldValueSplitData>) Splits all field value cells that match the specified condition. Inherited from PivotCustomFieldValueCellsEventArgsBase<T1, T2>.

Remarks

The CustomFieldValueCells event occurs when the layout of the Pivot Grid Control is changed, allowing you to customize column and row headers: field value cells, data field, total and grand total headers.

pivotgrid_FieldValueCellsAreas

Use the event parameter’s PivotCustomFieldValueCellsEventArgsBase<T1, T2>.GetCell method, to obtain data related to an individual cell, by its index. This method returns a FieldValueCell object, which provides the data. Use the PivotCustomFieldValueCellsEventArgsBase.GetCellCount method to obtain the total number of field value cells. Column/row headers can also be identified by their column/row. Use the PivotCustomFieldValueCellsEventArgsBase<T1, T2>.FindCell method to obtain the header whose column/row matches a specific condition.

The CustomFieldValueCells event allows you to specify the location of grand total headers using the PivotCustomFieldValueCellsEventArgsBase.SetGrandTotalLocation method. To obtain the current location of grand total headers, use the PivotCustomFieldValueCellsEventArgsBase.GetGrandTotalLocation method.

When handling the CustomFieldValueCells event, you can also remove individual cells with their nested columns and rows via the PivotCustomFieldValueCellsEventArgsBase.Remove method.

The PivotCustomFieldValueCellsEventArgsBase<T1, T2>.Split method allows you to split field value cells that have more than one nested cell. This method splits cells that match the specified condition (or, optionally, only the first matching cell) in a custom manner defined by the FieldValueSplitData objects.

Note

Custom values provided via the PivotGridControl.CustomCellValue, PivotGridControl.CustomSummary and PivotGridControl.CustomCellDisplayText events are not available when handling the CustomFieldValueCells event, because it is raised prior to these events.

Examples

Split Field Value Cells

The following example demonstrates how to split field value cells. In this example, the Grand Total column header is split into two cells: Price and Count.

View Example

Pivot Grid - split field value cells

Handle the CustomFieldValueCells event and call the event parameter’s Split method. Cells that should be split are identified by a predicate that returns true for those cells. The quantity, size, and captions of newly created cells are specified by an array of cell definitions (the FieldValueSplitData objects).

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraPivotGrid.Data;

namespace XtraPivotGrid_SplittingCells {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            pivotGridControl1.CustomFieldValueCells += 
                new PivotCustomFieldValueCellsEventHandler(pivotGrid_CustomFieldValueCells);
        }
        void Form1_Load(object sender, EventArgs e) {
            PivotHelper.FillPivot(pivotGridControl1);
            pivotGridControl1.DataSource = PivotHelper.GetDataTable();
            pivotGridControl1.BestFit();
        }
        protected void pivotGrid_CustomFieldValueCells(object sender,
                             PivotCustomFieldValueCellsEventArgs e) {
            PivotGridControl pivot = sender as PivotGridControl;
            if (pivot.DataSource == null) return;
            if (radioGroup1.SelectedIndex == 0) return;

            // Creates a predicate that returns true for the Grand Total headers, 
            // and false for any other column/row header.
            // Only cells that match this predicate are split.
            Predicate<FieldValueCell> condition =
                new Predicate<FieldValueCell>(delegate(FieldValueCell matchCell) {
                return matchCell.ValueType == PivotGridValueType.GrandTotal &&
                    matchCell.Field == null;
            });

            // Creates a list of cell definitions that represent newly created cells.
            // Two definitions are added to the list. The first one identifies the Price cell, 
            // which has two nested cells (the Retail Price and Wholesale Price
            // data field headers). The second one identifies the Count cell with 
            // one nested cell (the Quantity data field header).
            List<FieldValueSplitData> cells = new List<FieldValueSplitData>(2);
            cells.Add(new FieldValueSplitData("Price", 2));
            cells.Add(new FieldValueSplitData("Count", 1));

            // Performs splitting.
            e.Split(true, condition, cells);
        }
        void pivotGridControl1_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) {
            PivotGridControl pivot = sender as PivotGridControl;
            if (e.Field == pivot.Fields[PivotHelper.Month])
            {
                e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);
            }
        }
        void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) {
            this.pivotGridControl1.LayoutChanged();
        }        
    }
}

Hide Specific Rows and Columns

The following example demonstrates how handle the CustomFieldValueCells event to hide specific rows and columns. In this example, the event handler iterates through all row headers and removes rows that correspond to the “Employee B” field value, and that are not Total Rows.

View Example: How to Hide Specific Rows and Columns

Hide specific rows and columns

using System;
using System.Globalization;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;

namespace XtraPivotGrid_HidingColumnsAndRows {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            pivotGridControl1.CustomFieldValueCells += 
                new PivotCustomFieldValueCellsEventHandler(pivotGrid_CustomFieldValueCells);
        }
        void Form1_Load(object sender, EventArgs e) {
            PivotHelper.FillPivot(pivotGridControl1);
            pivotGridControl1.DataSource = PivotHelper.GetDataTable();
            pivotGridControl1.BestFit();
        }

        // Handles the CustomFieldValueCells event to remove
        // specific rows.
        protected void pivotGrid_CustomFieldValueCells(object sender,
                             PivotCustomFieldValueCellsEventArgs e) {

            PivotGridControl pivot = sender as PivotGridControl;
            if (pivot.DataSource == null) return;
            if (radioGroup1.SelectedIndex == 0) return;

            // Iterates through all row headers.
            for (int i = e.GetCellCount(false) - 1; i >= 0; i--) {
                FieldValueCell cell = e.GetCell(false, i);
                if (cell == null) continue;

                // If the current header corresponds to the "Employee B"
                // field value, and is not the Total Row header,
                // it is removed with all corresponding rows.
                if (object.Equals(cell.Value, "Employee B") &&
                    cell.ValueType != PivotGridValueType.Total)
                    e.Remove(cell);
            }
        }
        void pivotGridControl1_FieldValueDisplayText(object sender, 
                                    PivotFieldDisplayTextEventArgs e) {
            PivotGridControl pivot = sender as PivotGridControl;
            if (e.Field == pivot.Fields[PivotHelper.Month]) {
                e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);
            }
        }
        void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) {
            this.pivotGridControl1.LayoutChanged();
        }        
    }
}

Hide Empty Field Values (Columns/Rows)

The example below shows how to handle the CustomFieldValueCells event to hide empty columns and rows.

View Example: How to Hide Empty Field Values (Columns/Rows)

Hide empty field values

using System;
using System.Globalization;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;

namespace XtraPivotGrid_HidingColumnsAndRows {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            pivotGridControl1.CustomFieldValueCells += 
                new PivotCustomFieldValueCellsEventHandler(pivotGrid_CustomFieldValueCells);
        }
        void Form1_Load(object sender, EventArgs e) {
            PivotHelper.FillPivot(pivotGridControl1);
            pivotGridControl1.DataSource = PivotHelper.GetDataTable();
            pivotGridControl1.BestFit();
        }
        protected void pivotGrid_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e) {
            if (radioGroup1.SelectedIndex == 0) return;
            HideEmptyValues(true, e);
            HideEmptyValues(false, e);
        }
        private void HideEmptyValues(bool isColumn, PivotCustomFieldValueCellsEventArgs e) {
            for (int i = e.GetCellCount(isColumn) - 1; i >= 0; i--) {
                FieldValueCell cell = e.GetCell(isColumn, i);
                if (cell == null) continue;
                if (cell.EndLevel == e.GetLevelCount(isColumn) - 1) {
                    if (IsValueEmpty(isColumn, cell.MaxIndex, e)) {
                        e.Remove(cell);
                    }                        
                }
            }
        }
        private bool IsValueEmpty( bool isColumn, int valueIndex, PivotCustomFieldValueCellsEventArgs e) {
            if (isColumn)
                return IsCollumnEmpty(valueIndex, e);
            else
                return IsRowEmpty(valueIndex, e);
        }
        private bool IsRowEmpty(int rowIndex, PivotCustomFieldValueCellsEventArgs e) {
            for (int j = 0; j < e.ColumnCount ; j++) {
                decimal value = Convert.ToDecimal(e.GetCellValue(j, rowIndex));
                if (value != 0)
                    return false;
            }
            return true;
        }
        private bool IsCollumnEmpty(int columnIndex, PivotCustomFieldValueCellsEventArgs e) {

            for (int j = 0; j < e.RowCount; j++) {
                decimal value = Convert.ToDecimal(e.GetCellValue(columnIndex, j));
                if (value != 0)
                    return false;
            }
            return true;
        }
        void pivotGridControl1_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) {
            if (object.ReferenceEquals(e.Field, pivotGridControl1.Fields[PivotHelper.Month])) {
                e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);
            }
        }
        void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) {
            this.pivotGridControl1.LayoutChanged();
            pivotGridControl1.BestFit();
        }
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomFieldValueCells event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also