Back to Devexpress

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

aspnet-9206-components-pivot-grid-examples-data-shaping-how-to-add-and-remove-items-from-filter-drop-down-lists.md

latest6.3 KB
Original Source

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

  • Dec 17, 2020
  • 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.

aspx
<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1" 
OnCustomFilterPopupItems="ASPxPivotGrid1_CustomFilterPopupItems">
    <Fields>
        <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="1"
        Caption="Product Name" FieldName="ProductName">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedYear" Area="ColumnArea" AreaIndex="0" Caption="Year"
            FieldName="ShippedDate" GroupIndex="0" GroupInterval="DateYear" InnerGroupIndex="0">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldProductSales" Area="DataArea" AreaIndex="0" Caption="Sales"
            FieldName="ProductSales">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldCategoryName" Area="RowArea" AreaIndex="0"
        Caption="Category Name" FieldName="CategoryName">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedMonth" Area="ColumnArea" AreaIndex="2" Caption="Month"
            FieldName="ShippedDate" GroupIndex="0" InnerGroupIndex="2" GroupInterval="DateMonth">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedQuarter" Area="ColumnArea" AreaIndex="1"
        Caption="Quarter" FieldName="ShippedDate" GroupIndex="0"
        GroupInterval="DateQuarter" InnerGroupIndex="1"
        ValueFormat-FormatString="Quarter {0}"
        ValueFormat-FormatType="Custom">
        </dx:PivotGridField>
    </Fields>
    <Groups>
        <dx:PivotGridWebGroup />
    </Groups>
</dx:ASPxPivotGrid>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/nwind.mdb"
    SelectCommand="SELECT [CategoryName], [ProductName], [ProductSales],
    [ShippedDate] FROM [ProductReports]">
</asp:AccessDataSource>
csharp
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid.Data;

namespace ASPxPivotGrid_AddRemoveFilterItems {
    public partial class _Default : Page {
        readonly object dummyItem = "Dummy";
        protected void ASPxPivotGrid1_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)));
            }
        }
    }
}
vb
Imports DevExpress.Web.ASPxPivotGrid
Imports DevExpress.XtraPivotGrid.Data

Namespace ASPxPivotGrid_AddRemoveFilterItems
    Partial Public Class _Default
        Inherits Page
        Private ReadOnly dummyItem As Object = "Dummy"
        Protected Sub ASPxPivotGrid1_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
aspx
<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1" 
OnCustomFilterPopupItems="ASPxPivotGrid1_CustomFilterPopupItems">
    <Fields>
        <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="1"
        Caption="Product Name" FieldName="ProductName">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedYear" Area="ColumnArea" AreaIndex="0" Caption="Year"
            FieldName="ShippedDate" GroupIndex="0" GroupInterval="DateYear" InnerGroupIndex="0">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldProductSales" Area="DataArea" AreaIndex="0" Caption="Sales"
            FieldName="ProductSales">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldCategoryName" Area="RowArea" AreaIndex="0"
        Caption="Category Name" FieldName="CategoryName">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedMonth" Area="ColumnArea" AreaIndex="2" Caption="Month"
            FieldName="ShippedDate" GroupIndex="0" InnerGroupIndex="2" GroupInterval="DateMonth">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldShippedQuarter" Area="ColumnArea" AreaIndex="1"
        Caption="Quarter" FieldName="ShippedDate" GroupIndex="0"
        GroupInterval="DateQuarter" InnerGroupIndex="1"
        ValueFormat-FormatString="Quarter {0}"
        ValueFormat-FormatType="Custom">
        </dx:PivotGridField>
    </Fields>
    <Groups>
        <dx:PivotGridWebGroup />
    </Groups>
</dx:ASPxPivotGrid>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/nwind.mdb"
    SelectCommand="SELECT [CategoryName], [ProductName], [ProductSales],
    [ShippedDate] FROM [ProductReports]">
</asp:AccessDataSource>