Back to Devexpress

How to: Prevent End-Users From Changing Filter Conditions

wpf-9197-controls-and-libraries-pivot-grid-examples-grouping-filtering-and-summarizing-how-to-prevent-end-users-from-changing-filter-conditions.md

latest4.4 KB
Original Source

How to: Prevent End-Users From Changing Filter Conditions

  • Aug 01, 2019
  • 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.

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_CancelFilterChanging.MainWindow" Height="500" Width="650" Title="Main Window">
    <Grid>
        <dxpg:PivotGridControl FieldFilterChanging="pivotGridControl1_FieldFilterChanging" 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 DXPivotGrid_CancelFilterChanging.DataSet1TableAdapters

Namespace DXPivotGrid_CancelFilterChanging
    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 Sub pivotGridControl1_FieldFilterChanging(ByVal sender As Object, _
                    ByVal e As PivotFieldFilterChangingEventArgs)
            If Equals(e.Field, fieldCategoryName) Then
                If (e.Field.FilterValues.FilterType = FieldFilterType.Excluded AndAlso _
                            e.Values.Contains("Beverages")) OrElse _
                            (e.Field.FilterValues.FilterType = FieldFilterType.Included AndAlso _
                            (Not e.Values.Contains("Beverages"))) Then
                    MessageBox.Show("You are not allowed to hide the 'Beverages' value.")
                    e.Cancel = True
                End If
            End If
        End Sub
    End Class
End Namespace
csharp
using System.Windows;
using DevExpress.Xpf.PivotGrid;
using DXPivotGrid_CancelFilterChanging.DataSet1TableAdapters;

namespace DXPivotGrid_CancelFilterChanging {
    public partial class MainWindow : Window {
        DataSet1.ProductReportsDataTable productReportsDataTable =
            new DataSet1.ProductReportsDataTable();
        ProductReportsTableAdapter productReportsDataAdapter = new ProductReportsTableAdapter();
        public MainWindow() {
            InitializeComponent();
            pivotGridControl1.DataSource = productReportsDataAdapter.GetData();
        }
        private void pivotGridControl1_FieldFilterChanging(object sender,
                                        PivotFieldFilterChangingEventArgs e) {
            if (Equals(e.Field, fieldCategoryName)) {
                if ((e.Field.FilterValues.FilterType == FieldFilterType.Excluded &&
                        e.Values.Contains("Beverages")) ||
                    (e.Field.FilterValues.FilterType == FieldFilterType.Included &&
                        !e.Values.Contains("Beverages"))) {
                    MessageBox.Show("You are not allowed to hide the 'Beverages' value.");
                    e.Cancel = true;
                }
            }
        }
    }
}