Back to Devexpress

PivotCustomFieldValueCellsEventArgsBase<T1, T2>.FindCell(Boolean, Predicate<Object[]>) Method

corelibraries-devexpress-dot-xtrapivotgrid-dot-pivotcustomfieldvaluecellseventargsbase-2-dot-findcell-x28-system-dot-boolean-system-dot-predicate-system-dot-object-x29.md

latest8.6 KB
Original Source

PivotCustomFieldValueCellsEventArgsBase<T1, T2>.FindCell(Boolean, Predicate<Object[]>) Method

Returns the header of the column/row whose summary values match the specified condition.

Namespace : DevExpress.XtraPivotGrid

Assembly : DevExpress.PivotGrid.v25.2.Core.dll

NuGet Packages : DevExpress.PivotGrid.Core, DevExpress.Win.Navigation

Declaration

csharp
public T2 FindCell(
    bool isColumn,
    Predicate<object[]> match
)
vb
Public Function FindCell(
    isColumn As Boolean,
    match As Predicate(Of Object())
) As T2

Parameters

NameTypeDescription
isColumnBoolean

true to locate a column; false to locate a row.

| | match | Predicate<Object[]> |

A System.Predicate that specifies the condition used to locate the column/row.

|

Returns

TypeDescription
T2

A FieldValueCellBase class descendant that specifies the header of the column/row whose summary values match the specified predicate; null if no columns/rows match the predicate.

|

Remarks

Field value cells can also be obtained by their indexes via the PivotCustomFieldValueCellsEventArgsBase<T1, T2>.GetCell method.

Example

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.

csharp
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();
        }        
    }
}
vb
Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports DevExpress.XtraPivotGrid

Namespace XtraPivotGrid_FindCells
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
            AddHandler pivotGridControl1.CustomFieldValueCells, AddressOf pivotGrid_CustomFieldValueCells
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            PivotHelper.FillPivot(pivotGridControl1)
            pivotGridControl1.DataSource = PivotHelper.GetDataTable()
            pivotGridControl1.BestFit()
        End Sub

        ' Handles the CustomFieldValueCells event to remove columns with
        ' zero summary values.
        Protected Sub pivotGrid_CustomFieldValueCells(ByVal sender As Object, 
                            ByVal e As PivotCustomFieldValueCellsEventArgs)
            Dim pivot As PivotGridControl = TryCast(sender, PivotGridControl)
            If pivot.DataSource Is Nothing Then
                Return
            End If
            If radioGroup1.SelectedIndex = 0 Then
                Return
            End If

            ' Obtains the first encountered column header whose column
            ' matches the specified condition, represented by a predicate.
            Dim cell As FieldValueCell = e.FindCell(True,
                            New Predicate(Of Object())(Function(dataCellValues() As Object)
                ' Defines the predicate returning true for columns
                ' that contain only zero summary values.
                For Each value As Object In dataCellValues
                    If Not Object.Equals(CDec(0), value) Then
                        Return False
                    End If
                Next value
                Return True
            End Function))

            ' If any column header matches the condition, this column is removed.
            If cell IsNot Nothing Then
                e.Remove(cell)
            End If
        End Sub
        Private Sub pivotGridControl1_FieldValueDisplayText(ByVal sender As Object,
                       ByVal e As PivotFieldDisplayTextEventArgs) Handles pivotGridControl1.FieldValueDisplayText
            Dim pivot As PivotGridControl = TryCast(sender, PivotGridControl)
            If e.Field Is pivot.Fields(PivotHelper.Month) Then
                e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CInt((e.Value)))
            End If
        End Sub
        Private Sub radioGroup1_SelectedIndexChanged(ByVal sender As Object,
                            ByVal e As EventArgs) Handles radioGroup1.SelectedIndexChanged
            Me.pivotGridControl1.LayoutChanged()
        End Sub
    End Class
End Namespace

See Also

GetCell(Boolean, Int32)

PivotCustomFieldValueCellsEventArgsBase<T1, T2> Class

PivotCustomFieldValueCellsEventArgsBase<T1, T2> Members

DevExpress.XtraPivotGrid Namespace