How to: Prevent End-Users From Changing Filter Conditions

  • 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.

using System;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;

namespace EmptyWinApp {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            this.productReportsTableAdapter.Fill(this.productReports._ProductReports);
        }
        private void pivotGridControl1_FieldFilterChanging(object sender,
                PivotFieldFilterChangingEventArgs e) {
            if(object.ReferenceEquals(e.Field, fieldCategoryName)) {
                if((e.Field.FilterValues.FilterType == PivotFilterType.Excluded && 
                        e.Values.Contains("Beverages")) ||
                    (e.Field.FilterValues.FilterType == PivotFilterType.Included &&
                        !e.Values.Contains("Beverages"))) {
                    MessageBox.Show("You are not allowed to hide the 'Beverages' value.");
                    e.Cancel = true;
                }
            }
        }
    }
}