The following example demonstrates how to sort filter drop-down items in a custom manner.
In this example, the CustomFilterPopupItems event is handled to manage a list of filter items. This event is raised when an end-user invokes the filter drop-down. The event parameter provides access to the filter items list, which is then sorted by the length of the items' captions (if the corresponding option is selected).
View Example
using System;
using System.Collections;
using System.Web.UI;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid.Data;
namespace ASPxPivotGrid_CustomFilterItemsSorting {
public partial class _Default : Page {
protected void ASPxPivotGrid1_CustomFilterPopupItems(object sender,
PivotCustomFilterPopupItemsEventArgs e) {
if (DropDownList1.Items[1].Selected)
ArrayList.Adapter((IList)e.Items).Sort(new FilterItemsComparer());
}
}
public class FilterItemsComparer : IComparer {
int IComparer.Compare(object x, object y) {
if (!(x is PivotGridFilterItem) || !(y is PivotGridFilterItem)) return 0;
PivotGridFilterItem item1 = (PivotGridFilterItem)x;
PivotGridFilterItem item2 = (PivotGridFilterItem)y;
if (item1.ToString().Length == item2.ToString().Length) return 0;
if (item1.ToString().Length > item2.ToString().Length) return 1;
return -1;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs"
Inherits="ASPxPivotGrid_CustomFilterItemsSorting._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.3.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxPivotGrid"
TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="padding-bottom:5px">
<asp:Label ID="ASPxLabel1" runat="server" Text="Sort Filter Items">
</asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Alphabetically" Selected="true"/>
<asp:ListItem Text="By Caption Length"/>
</asp:DropDownList>
</div>
<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>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Web.UI
Imports DevExpress.Web.ASPxPivotGrid
Imports DevExpress.XtraPivotGrid.Data
Namespace ASPxPivotGrid_CustomFilterItemsSorting
Partial Public Class _Default
Inherits Page
Protected Sub ASPxPivotGrid1_CustomFilterPopupItems(ByVal sender As Object, _
ByVal e As PivotCustomFilterPopupItemsEventArgs)
If DropDownList1.Items(1).Selected Then
ArrayList.Adapter(CType(e.Items, IList)).Sort(New FilterItemsComparer())
End If
End Sub
End Class
Public Class FilterItemsComparer
Implements IComparer
Private Function IComparer_Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
If Not(TypeOf x Is PivotGridFilterItem) OrElse Not(TypeOf y Is PivotGridFilterItem) Then
Return 0
End If
Dim item1 As PivotGridFilterItem = CType(x, PivotGridFilterItem)
Dim item2 As PivotGridFilterItem = CType(y, PivotGridFilterItem)
If item1.ToString().Length = item2.ToString().Length Then
Return 0
End If
If item1.ToString().Length > item2.ToString().Length Then
Return 1
End If
Return -1
End Function
End Class
End Namespace
<%@ Page Language="vb" AutoEventWireup="true" Codebehind="Default.aspx.vb"
Inherits="ASPxPivotGrid_CustomFilterItemsSorting._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.3.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxPivotGrid"
TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="padding-bottom:5px">
<asp:Label ID="ASPxLabel1" runat="server" Text="Sort Filter Items">
</asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Alphabetically" Selected="true"/>
<asp:ListItem Text="By Caption Length"/>
</asp:DropDownList>
</div>
<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>
</div>
</form>
</body>
</html>