Skip to main content
A newer version of this page is available. .

How to: Locate a Column (Row) Header By Its Summary Values

  • 3 minutes to read

The following example demonstrates how to handle the CustomFieldValueCells event to locate a specific column/row header identified by its column’s/row’s summary values.

In this example, a predicate is used to locate a column that contains only zero summary values. The column header is obtained by the event parameter’s FindCell method, and then removed via the Remove method.

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

namespace XtraPivotGrid_FindCells {
    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 columns with
        // zero summary values.
        protected void pivotGrid_CustomFieldValueCells(object sender,
                                     PivotCustomFieldValueCellsEventArgs e)
        {
            PivotGridControl pivot = sender as PivotGridControl;
            if (pivot.DataSource == null) return;
            if (radioGroup1.SelectedIndex == 0) return;

            // Obtains the first encountered column header whose column
            // matches the specified condition, represented by a predicate.
            FieldValueCell cell = e.FindCell(true, new Predicate<object[]>(

                // Defines the predicate returning true for columns
                // that contain only zero summary values.
                delegate(object[] dataCellValues)
                {
                    foreach (object value in dataCellValues)
                    {
                        if (!object.Equals((decimal)0, value))
                            return false;
                    }
                    return true;
                }));

            // If any column header matches the condition, this column is removed.
            if (cell != null) 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();
        }        
    }
}