Back to Devexpress

How to: Prevent End-Users From Changing Filter Conditions

aspnet-9268-components-pivot-grid-examples-data-shaping-how-to-prevent-end-users-from-changing-filter-conditions.md

latest6.2 KB
Original Source

How to: Prevent End-Users From Changing Filter Conditions

  • Dec 17, 2020
  • 2 minutes to read

The following example shows how to prevent end-users from changing the filter condition.In this example, the FieldFilterChanging event is handled to prevent an end-user from hiding the 'Beverages' field value. If an end-user tries to hide the 'Beverages' field value, the event handler sets the event parameter's Cancel property to true to cancel changing the filter condition.

aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="ASPxPivotGrid_CancelFilterChange._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.4.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>
        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
        OnFieldFilterChanging="ASPxPivotGrid1_FieldFilterChanging">
            <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" 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>
            </Fields>
        </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>
csharp
using System.Web.UI;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;

namespace ASPxPivotGrid_CancelFilterChange {
    public partial class _Default : Page {
        protected void ASPxPivotGrid1_FieldFilterChanging(object sender, 
                                    PivotFieldFilterChangingEventArgs e) {
            if (Equals(e.Field, fieldCategoryName)) {
                if ((e.Field.FilterValues.FilterType == PivotFilterType.Excluded &&
                        e.Values.Contains("Beverages")) ||
                    (e.Field.FilterValues.FilterType == PivotFilterType.Included &&
                        !e.Values.Contains("Beverages"))) {
                    e.Cancel = true;
                }
            }
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System.Web.UI
Imports DevExpress.Web.ASPxPivotGrid
Imports DevExpress.XtraPivotGrid

Namespace ASPxPivotGrid_CancelFilterChange
    Partial Public Class _Default
        Inherits Page
        Protected Sub ASPxPivotGrid1_FieldFilterChanging(ByVal sender As Object, _
                ByVal e As PivotFieldFilterChangingEventArgs)
            If Equals(e.Field, fieldCategoryName) Then
                If (e.Field.FilterValues.FilterType = PivotFilterType.Excluded AndAlso _
                        e.Values.Contains("Beverages")) OrElse _
                    (e.Field.FilterValues.FilterType = PivotFilterType.Included AndAlso _
                        (Not e.Values.Contains("Beverages"))) Then
                    e.Cancel = True
                End If
            End If
        End Sub
    End Class
End Namespace
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb"
    Inherits="ASPxPivotGrid_CancelFilterChange._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.4.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>
        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
        OnFieldFilterChanging="ASPxPivotGrid1_FieldFilterChanging">
            <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" 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>
            </Fields>
        </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>