Back to Devexpress

PivotCustomFieldValueCellsEventArgs.FindCell(Boolean, Predicate<Object[]>) Method

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotcustomfieldvaluecellseventargs-dot-findcell-x28-system-dot-boolean-system-dot-predicate-system-dot-object-x29.md

latest9.1 KB
Original Source

PivotCustomFieldValueCellsEventArgs.FindCell(Boolean, Predicate<Object[]>) Method

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

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

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

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
FieldValueCell

A FieldValueCell object representing 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 using the PivotCustomFieldValueCellsEventArgs.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.

vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Globalization
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid

Namespace DXPivotGrid_FindCells
    Partial Public Class Window1
        Inherits Window
        Public Sub New()
            InitializeComponent()
            AddHandler pivotGrid.CustomFieldValueCells, AddressOf pivotGrid_CustomFieldValueCells
        End Sub
        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            PivotHelper.FillPivot(pivotGrid)
            pivotGrid.DataSource = PivotHelper.GetDataTable()
            pivotGrid.BestFit()
        End Sub

        ' Handles the CustomFieldValueCells event to remove columns with
        ' zero summary values.
        Private Sub pivotGrid_CustomFieldValueCells(ByVal sender As Object, _
                                                    ByVal e As PivotCustomFieldValueCellsEventArgs)
            If pivotGrid.DataSource Is Nothing Then
                Return
            End If
            If rbDefault.IsChecked = True 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) AnonymousMethod1(dataCellValues)))

            ' If any column header matches the condition, this column is removed.
            If cell IsNot Nothing Then
                e.Remove(cell)
            End If
        End Sub

        ' Defines the predicate returning true for columns
        ' that contain only zero summary values.
        Private Function AnonymousMethod1(ByVal dataCellValues() As Object) As Boolean
            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
        Private Sub pivotGrid_FieldValueDisplayText(ByVal sender As Object, _
                                                    ByVal e As PivotFieldDisplayTextEventArgs)
            If Object.Equals(e.Field, pivotGrid.Fields(PivotHelper.Month)) Then
                e.DisplayText = _
                    CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CInt(Fix(e.Value)))
            End If
        End Sub
        Private Sub rbDefault_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
            pivotGrid.LayoutChanged()
        End Sub
    End Class
End Namespace
xaml
<Window xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="DXPivotGrid_FindCells.Window1" 
        dx:ThemeManager.ThemeName="LightGray" 
        Height="580" Width="1200"
        Loaded="Window_Loaded"
        Title="Window1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <dxpg:PivotGridControl Grid.Row="1" Margin="2,2,2,0" Name="pivotGrid" AllowDrag="False"
                               FieldValueDisplayText="pivotGrid_FieldValueDisplayText" 
                               AllowFilter="False" />
        <GroupBox Grid.Row="0" Height="Auto" Margin="2,2,2,0"
                  Name="groupBox1" VerticalAlignment="Bottom">
            <StackPanel Orientation="Vertical">
                <RadioButton x:Name="rbDefault" IsChecked="True" Content="Default Layout"
                             Margin="0,0,0,2" Checked="rbDefault_Checked" />
                <RadioButton Checked="rbDefault_Checked" Margin="0,2,0,0"
                    Content="Remove Columns with Zero Summary Values" />
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>
csharp
using System;
using System.Globalization;
using System.Windows;
using DevExpress.Xpf.PivotGrid;

namespace DXPivotGrid_FindCells {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            pivotGrid.CustomFieldValueCells += 
                new PivotCustomFieldValueCellsEventHandler(pivotGrid_CustomFieldValueCells);
        }
        void Window_Loaded(object sender, RoutedEventArgs e) {
            PivotHelper.FillPivot(pivotGrid);
            pivotGrid.DataSource = PivotHelper.GetDataTable();
            pivotGrid.BestFit();
        }

        // Handles the CustomFieldValueCells event to remove columns with
        // zero summary values.
        void pivotGrid_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e) {
            if (pivotGrid.DataSource == null) return;
            if (rbDefault.IsChecked == true) 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 pivotGrid_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) {
            if(e.Field == pivotGrid.Fields[PivotHelper.Month]) {
                e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);
            }
        }
        private void rbDefault_Checked(object sender, RoutedEventArgs e) {
            pivotGrid.LayoutChanged();
        }
    }
}

See Also

GetCell(Boolean, Int32)

PivotCustomFieldValueCellsEventArgs Class

PivotCustomFieldValueCellsEventArgs Members

DevExpress.Xpf.PivotGrid Namespace