Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office / MS-Excel

Show only visible values in RadGridView filter popup

5.00/5 (1 vote)
7 Aug 2013CPOL 8.6K  
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)