Back to Devexpress

How to: Implement the Group Filter

wpf-8830-controls-and-libraries-pivot-grid-examples-grouping-filtering-and-summarizing-how-to-implement-the-group-filter.md

latest5.3 KB
Original Source

How to: Implement the Group Filter

  • Aug 01, 2019
  • 2 minutes to read

This example demonstrates how to apply custom group filters to PivotGridControl data.

In this example, a filter type is defined by setting the PivotGroupFilterValues.FilterType property to PivotFilterType.Included. To create and add group filter values to the PivotGroupFilterValues.Values and PivotGroupFilterValue.ChildValues collections the PivotGroupFilterValuesCollection.Add method is used.

vb
Imports Microsoft.VisualBasic
Imports System.Windows
Imports GroupFilter.nwindDataSetTableAdapters
Imports DevExpress.Xpf.PivotGrid

Namespace GroupFilter
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
            Dim salesPersonDataAdapter As New SalesPersonTableAdapter()

            ' Binds the pivot grid to data.
            pivotGridControl1.DataSource = salesPersonDataAdapter.GetData()

            ' Creates a group and adds two fields to it.
            Dim OrderDateGroup As PivotGridGroup = pivotGridControl1.Groups.Add( _
                pivotGridControl1.Fields(2), pivotGridControl1.Fields(3))

            ' Locks the PivotGroupFilterValues object by disabling visual updates.
            OrderDateGroup.FilterValues.BeginUpdate()

            ' Sets a filter type. 
            ' It specifies that the PivotGridControl should display only filter values.
            OrderDateGroup.FilterValues.FilterType = FieldFilterType.Included

            ' Creates a filter value and adds it to the PivotGroupFilterValues.Values collection.
            OrderDateGroup.FilterValues.Values.Add(1994)

            ' Creates a filter value and adds it to the PivotGroupFilterValues.Values collection.
            ' Then creates a child value of the filter value and adds it to the parent value 
            ' collection.
            OrderDateGroup.FilterValues.Values.Add(1995).ChildValues.Add(1)

            ' Unlocks the PivotGroupFilterValues object.
            OrderDateGroup.FilterValues.EndUpdate()
        End Sub
    End Class
End Namespace
csharp
using System.Windows;
using GroupFilter.nwindDataSetTableAdapters;
using DevExpress.Xpf.PivotGrid;

namespace GroupFilter {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            SalesPersonTableAdapter salesPersonDataAdapter = new SalesPersonTableAdapter();

            // Binds the pivot grid to data.
            pivotGridControl1.DataSource = salesPersonDataAdapter.GetData();

            // Creates a group and adds two fields to it.
            PivotGridGroup OrderDateGroup = pivotGridControl1.Groups.Add(pivotGridControl1.Fields[2],
                pivotGridControl1.Fields[3]);

            // Locks the PivotGroupFilterValues object by disabling visual updates.
            OrderDateGroup.FilterValues.BeginUpdate();

            // Sets a filter type. 
            // It specifies that the PivotGridControl should display only filter values.
            OrderDateGroup.FilterValues.FilterType = FieldFilterType.Included;

            // Creates a filter value and adds it to the PivotGroupFilterValues.Values collection.
            OrderDateGroup.FilterValues.Values.Add(1994);

            // Creates a filter value and adds it to the PivotGroupFilterValues.Values collection.
            // Then creates a child value of the filter value and adds it to the parent value 
            // collection.
            OrderDateGroup.FilterValues.Values.Add(1995).ChildValues.Add(1);

            // Unlocks the PivotGroupFilterValues object.
            OrderDateGroup.FilterValues.EndUpdate();
        }
    }
}
xaml
<Window x:Class="GroupFilter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
        xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid">
    <Grid>
        <dxpg:PivotGridControl Name="pivotGridControl1" BestFitMode="AllRows">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Area="DataArea" FieldName="Extended Price" CellFormat="c0" />
                <dxpg:PivotGridField Area="RowArea" 
                                     Caption="Product Category" FieldName="CategoryName" />
                <dxpg:PivotGridField Area="ColumnArea" 
                                     Caption="Year" FieldName="OrderDate" GroupInterval="DateYear" />
                <dxpg:PivotGridField Area="ColumnArea" 
                                     Caption="Month" FieldName="OrderDate" GroupInterval="DateMonth" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
    </Grid>
</Window>