The following example demonstrates how to hide particular rows and columns by handling the CustomFieldValueCells event.
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.
C#:Window1.xaml.cs |
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();
}
void pivotGrid_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e) {
if (pivotGrid.DataSource == null) return;
if (rbDefault.IsChecked == true) return;
for (int i = e.GetCellCount(false) - 1; i >= 0; i--) {
FieldValueCell cell = e.GetCell(false, i);
if (cell == null) continue;
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();
}
}
}
|
VB:Window1.xaml.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
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
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 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:Window1.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>
|