Blogs

Paul Kimmel's Blog

Windows Forms Pivot Grid Control – Using the CustomFilterPopupItems Event to Manage Filter Items in 10.2

     

I manage a TODO list with Microsoft Outlook. If it weren’t for Outlook and Calendar items synched on my iPhone I would forgot a lot of detailed tasks. Right now there are so many new features for 10.2 that I want to cover that my task list has gotten pretty long and is growing and projecting out for several weeks to come. It is long enough right now that I wonder if I am going to get some sleep this weekend or get to that 30 foot trunk of a tree that the wind just knocked over into my yard. The last week or so sleep has been at the bottom of the TODO list—although I did wheeze through 30 minutes of basketball at the gym last night.

The feature on the menu today is a single feature, the CustomFilterPopupItems event for the XtraPivotGrid. The XtraPivotGrid is a composable, searchable, sortable, filterable grid that supports data mining type tasks for your end users. The CustomFilterPopupItems event is new in 10.2 and is fired when the user displays the filter list for a field. Figure 1 shows the demo application with the filter list visible. A user brings up the filter list by clicking on the little filter button associated with a given field.

Pivot Grid Control with Beverages Category Checked
Figure 1: Pivot Grid Control with the filter items visible for the Category Name field. 

This demo handles the CustomFilterPopupItems event for the XtraPivotGrid control. Normally when you display the filter and deselect items those elements are removed from the result set—the data area in the grid and the row area. For example, if you expand the Category Name filter and uncheck Show All and check Beverages then the default behavior is that the products that are beverages are the only products shown .

By default the non-visible data items are available in the filter still. This sample demonstrates how to hide the checked filter items that aren’t visible in the grid. For example, in our scenario by checking the Beverages Category only beverages are visible in the grid, and with the sample code for CustomFilterPopupItems in Listing 1 the invisible items are also removed from the filter list. For instance, if you drop the Category Name filter in our scenario then the event code removes items that for all intents and purposes won’t be displayed anyway.

Figure 2 shows the Beverages scenario without the event code, and Figure 3 shows the scenario with the event code.

Without the CustomFilterPopupItems Event All Products are Listed in the Filter  
Figure 2: Notice that even though Beverages is the only Category checked all products are visible in the Product Name filter.

With the CustomFilterPopupItems Event Only Beverages are Shon in the Product Filter
Figure 3: With the CustomFilterPopupItems event (as written in Listing 1) invisible items are removed from the popup filter list.

Listing 1: Using the CustomFilterPopupItems event to hide filter items that aren’t being displayed in the data grid based on other filter selections.

using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid.Data;

namespace EmptyWinApp {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // TODO: This line of code loads data into the 'productReports._ProductReports' 
            // table. You can move, or remove it, as needed.
            this.productReportsTableAdapter.Fill(this.productReports._ProductReports);
        }

        private void pivotGridControl1_CustomFilterPopupItems(object sender, 
        DevExpress.XtraPivotGrid.PivotCustomFilterPopupItemsEventArgs e) 
        {
            List<object> values = e.Field.GetVisibleValues();
            values.Sort();
            for(int i = e.Items.Count - 1; i >= 0; i--) {
                if(e.Items[i].IsChecked == true && values.BinarySearch(e.Items[i].Value) < 0)
                    e.Items.RemoveAt(i);
            }
        }
    }
}

Comments

 

Paul Fuller said:

Is that a spurious 'return;' on the first line of the event handler?

September 4, 2010 1:49 AM
 

Bruno Cossi said:

This is very helpful, looking forward to it!

Will the same event be available in the Wpf version?

September 4, 2010 12:34 PM
 

Paul Kimmel (DevExpress) said:

The return was to demonstrate the default behavior without the event. You can toggle it with a comment to see the difference. I removed it from the listing though.

September 6, 2010 5:49 PM
 

Carl Pritchard said:

Will be much appreciated as the normal behaviour has been a source of frustration (for me via my end user's badgering).

I would, however, vote for this being the default native behaviour of the control rather than needing extra implementation. Have never seen the logic (neither do end users) of having items visible that would not apply with preceeding filters in place.

September 7, 2010 8:51 AM
 

Ivan N (DevExpress R&D) said:

2Bruno: Yes, ASP.NET and WPF will support it.

2Carl: Not sure that we will be able to do this without breaking current applications. If we make it default, there will be no way to return the previous behavior if necessary. Also, I'm 100% sure that there will be at least one customer who will disagree with the new behavior.

Probably, we will make it possible to turn the new behavior in a simpler manner.

September 8, 2010 9:42 AM
 

Brian Maxim said:

Looks good. I'll be putting this to use.

September 23, 2010 7:17 AM
 

Francesc Diaz said:

Perfect! That's exactly what I was looking for!

Great as always!

May 24, 2011 9:18 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.