Click here to Skip to main content
15,891,473 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Show only visible values in RadGridView filter popup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Aug 2013CPOL 8.5K   2  
Show only visible values in RadGridView filter popup.

Introduction 

Excel-like filtering in RadGridView for WinForms does not work completely Excel-like. In fact, whatever is filtered currently on the screen, the filter popup displays all the values always. Here is a short code to display only values that appear on the screen in a particular column, just as in Excel.

Date columns are not considered at the moment. 

Using the code

Add an event handler for the FilterPopupRequired event with the code below: 

C#
private void radGridView1_FilterPopupRequired(object sender, FilterPopupRequiredEventArgs e)
{
    if (e.FilterPopup is RadListFilterPopup)
    {
        RadListFilterPopup ePopup = (RadListFilterPopup)e.FilterPopup;

        List<object> childList = new List<object>();

        foreach (var row in this.radGridView1.ChildRows)
        {
            var value = row.Cells[e.Column.Index].Value;
            if (!childList.Contains(value)) childList.Add(value);
        }

        RadListFilterPopup newPopup = new RadListFilterPopup(e.Column);

        foreach (System.Collections.ArrayList item in 
                  ePopup.MenuTreeElement.DistinctListValues.Values)
        {
            if (!childList.Contains(item[0]))
            {
                newPopup.MenuTreeElement.DistinctListValues.Remove(item[0].ToString());
            }
        }

        e.FilterPopup = newPopup;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Kodar Engineering
Serbia Serbia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --