Back to Devexpress

PivotCustomFieldValueCellsEventArgs.Remove(FieldValueCell) Method

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotcustomfieldvaluecellseventargs-dot-remove-x28-devexpress-dot-xpf-dot-pivotgrid-dot-fieldvaluecell-x29.md

latest10.7 KB
Original Source

PivotCustomFieldValueCellsEventArgs.Remove(FieldValueCell) Method

Removes the specified field value cell.

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public bool Remove(
    FieldValueCell item
)
vb
Public Function Remove(
    item As FieldValueCell
) As Boolean

Parameters

NameTypeDescription
itemFieldValueCell

A FieldValueCell object that represents the field value cell to remove.

|

Returns

TypeDescription
Boolean

true if the specified cell has been found and removed; false if the specified cell has not been found.

|

Remarks

To locate the required field value cell, use the PivotCustomFieldValueCellsEventArgs.GetCell or PivotCustomFieldValueCellsEventArgs.FindCell method.

Note that if a field value cell has only one child cell, and this child cell is removed via the Remove method, the parent cell is then automatically removed.

Example

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

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

Namespace DXPivotGrid_HidingColumnsAndRows
    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
        ' specific rows.
        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

            ' Iterates through all row headers.
            For i As Integer = e.GetCellCount(False) - 1 To 0 Step -1
                Dim cell As FieldValueCell = e.GetCell(False, i)
                If cell Is Nothing Then Continue For

                ' 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") AndAlso _
                        cell.ValueType <> FieldValueType.Total Then
                    e.Remove(cell)
                End If
            Next i
        End Sub
        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_HidingColumnsAndRows.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="Delete All Rows Corresponding to 'Employee B' except for the Total Row" />
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>
csharp
using System.Globalization;
using System.Windows;
using DevExpress.Xpf.PivotGrid;

namespace DXPivotGrid_HidingColumnsAndRows {
    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
        // specific rows.
        void pivotGrid_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e) {
            if (pivotGrid.DataSource == null) return;
            if (rbDefault.IsChecked == true) 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 != FieldValueType.Total)
                    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();
        }
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the Remove(FieldValueCell) method.

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.

wpf-pivot-grid-hide-specific-columns-and-row/CS/WpfApp/MainWindow.xaml.cs#L34

csharp
cell.ValueType != FieldValueType.Total)
        e.Remove(cell);
}

wpf-pivot-grid-add-custom-field-values-rows-columns-not-present-in-datasource/CS/WpfApplication1/MainWindow.xaml.cs#L64

csharp
if (index != -1)
        e.Remove(e.GetCell(false, index));
}

wpf-pivot-grid-hide-specific-columns-and-row/VB/WpfApp/MainWindow.xaml.vb#L43

vb
If Object.Equals(cell.Value, "Employee B") AndAlso cell.ValueType <> FieldValueType.Total Then
    e.Remove(cell)
End If

wpf-pivot-grid-add-custom-field-values-rows-columns-not-present-in-datasource/VB/WpfApplication1/MainWindow.xaml.vb#L48

vb
Dim index As Integer = Me.pivot.GetRowIndex(New Object() {Nothing})
    If index <> -1 Then e.Remove(e.GetCell(False, index))
End Sub

See Also

GetCell(Boolean, Int32)

FindCell(Boolean, Predicate<Object[]>)

PivotCustomFieldValueCellsEventArgs Class

PivotCustomFieldValueCellsEventArgs Members

DevExpress.Xpf.PivotGrid Namespace