Back to Devexpress

How to: Add and Remove Items From Filter Drop-Down Lists

wpf-9198-controls-and-libraries-pivot-grid-examples-grouping-filtering-and-summarizing-how-to-add-and-remove-items-from-filter-drop-down-lists.md

latest6.2 KB
Original Source

How to: Add and Remove Items From Filter Drop-Down Lists

  • Aug 01, 2019
  • 2 minutes to read

The following example shows how to add and remove items from the filter dropdown list.In this example, the 'Beverages' item is hidden from the filter dropdown list of the Category field, and a dummy item is created and added to the list. To do this, the CustomFilterPopupItems event is handled. In the event handler, the 'Beverages' item is removed from the event parameter's Items collection, and a new item ('Dummy Item') is added to the collection.

csharp
using System.Windows;
using DevExpress.Xpf.PivotGrid;
using DevExpress.XtraPivotGrid.Data;
using DXPivotGrid_CustomFilterItems.DataSet1TableAdapters;

namespace DXPivotGrid_CustomFilterItems {
    public partial class MainWindow : Window {
        DataSet1.ProductReportsDataTable productReportsDataTable =
            new DataSet1.ProductReportsDataTable();
        ProductReportsTableAdapter productReportsDataAdapter = new ProductReportsTableAdapter();
        public MainWindow() {
            InitializeComponent();
            pivotGridControl1.DataSource = productReportsDataAdapter.GetData();
        }
        readonly string dummyItem = "";
        private void pivotGridControl1_CustomFilterPopupItems(object sender,
                                    PivotCustomFilterPopupItemsEventArgs e) {
            if (object.ReferenceEquals(e.Field, fieldCategoryName)) {
                for (int i = e.Items.Count - 1; i >= 0; i--) {
                    if (object.Equals(e.Items[i].Value, "Beverages")) {
                        e.Items.RemoveAt(i);
                        break;
                    }
                }
                e.Items.Insert(0, new PivotGridFilterItem(dummyItem,
                                                          "Dummy Item",
                                                          e.Field.FilterValues.Contains(dummyItem)));
            }
        }
    }
}
xaml
<Window xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="DXPivotGrid_CustomFilterItems.MainWindow"
        Height="500" Width="600" Title="Main Window">
    <Grid>
        <dxpg:PivotGridControl CustomFilterPopupItems="pivotGridControl1_CustomFilterPopupItems"
                               Name="pivotGridControl1" >
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Name="fieldProductSales" FieldName="ProductSales"
                                     Area="DataArea" Caption="Product Sales"/>
                <dxpg:PivotGridField Name="fieldCategoryName" FieldName="CategoryName" Area="RowArea"
                                     AreaIndex="0" Caption="Category Name"/>
                <dxpg:PivotGridField Name="fieldProductName" FieldName="ProductName" Area="RowArea"
                                     AreaIndex="1" Caption="Product Name"/>
                <dxpg:PivotGridField Name="fieldShippedDate" FieldName="ShippedDate" Area="ColumnArea"
                                     GroupInterval="DateYear" Caption="Year"/>
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
    </Grid>
</Window>
xaml
<Window xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="DXPivotGrid_CustomFilterItems.MainWindow" Height="500" Width="600" Title="Main Window">
    <Grid>
        <dxpg:PivotGridControl CustomFilterPopupItems="pivotGridControl1_CustomFilterPopupItems" Name="pivotGridControl1">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Name="fieldProductSales" FieldName="ProductSales" Area="DataArea" Caption="Product Sales" />
                <dxpg:PivotGridField Name="fieldCategoryName" FieldName="CategoryName" Area="RowArea" AreaIndex="0" Caption="Category Name" />
                <dxpg:PivotGridField Name="fieldProductName" FieldName="ProductName" Area="RowArea" AreaIndex="1" Caption="Product Name" />
                <dxpg:PivotGridField Name="fieldShippedDate" FieldName="ShippedDate" Area="ColumnArea" GroupInterval="DateYear" Caption="Year" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
    </Grid>
</Window>
vb
Imports Microsoft.VisualBasic
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid
Imports DevExpress.XtraPivotGrid.Data
Imports DXPivotGrid_CustomFilterItems.DataSet1TableAdapters

Namespace DXPivotGrid_CustomFilterItems
    Partial Public Class MainWindow
        Inherits Window
        Private productReportsDataTable As New DataSet1.ProductReportsDataTable()
        Private productReportsDataAdapter As New ProductReportsTableAdapter()
        Public Sub New()
            InitializeComponent()
            pivotGridControl1.DataSource = productReportsDataAdapter.GetData()
        End Sub
        Private ReadOnly dummyItem As String = ""
        Private Sub pivotGridControl1_CustomFilterPopupItems(ByVal sender As Object, _
                    ByVal e As PivotCustomFilterPopupItemsEventArgs)
            If Object.ReferenceEquals(e.Field, fieldCategoryName) Then
                For i As Integer = e.Items.Count - 1 To 0 Step -1
                    If Object.Equals(e.Items(i).Value, "Beverages") Then
                        e.Items.RemoveAt(i)
                        Exit For
                    End If
                Next i
                e.Items.Insert(0, New PivotGridFilterItem(dummyItem, _
                            "Dummy Item", _
                            e.Field.FilterValues.Contains(dummyItem)))
            End If
        End Sub
    End Class
End Namespace