65.9K
CodeProject is changing. Read more.
Home

Show only visible values in RadGridView filter popup

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Aug 7, 2013

CPOL
viewsIcon

8640

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: 

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;
    }
}