views:

813

answers:

2

I have a datasource (BindingList) with many Users, but there are some users I don't want to display in my DataGridView. Is it possible to hide them. I can't find events that works.

RowsAdded sometimes hides worng rows.

+2  A: 

You can filter the rows with the BindingSource.Filter property. However, the built-in implementation of BindingList<T> doesn't support filtering, so you need to implement it yourself. You can find a few examples on Google. This one looks interesting...

Thomas Levesque
A: 

Looks like I have to implement my own filter. I build an adapter for the BindingList wich can display a filtered version of any BindingList. You just has to inherite from it. Here is my Example. I want to show only users with user.CanEdit = true

public class AhpUserFilter : FilterBindingListAdapter<AhpUser>
{

    public AhpUserFilter(AhpUserCollection users)
        : base(users.GetList() as IBindingList)
    {

    }

    protected override bool ISVisible(AhpUser user)
    {
        return user.CanEdit;
    }
}

Here is how you can bind the new List to a DatagridView:

AhpUserFilter userSource = new AhpUserFilter(users);
userSource.Filter = "yes!";

dataGridViewUser.DataSource = userSource;

Okay, the Filter property is useless, yet. But the Adapter class is very experimental yet. But for simple adding and removing with a DataGrid it seems to work well.

Here is the code for the adapter:

public class FilterBindingListAdapter<T> : BindingList<T>, IBindingListView
{
    protected string filter = String.Empty;
    protected IBindingList bindingList;
    private bool filtering = false;

    public FilterBindingListAdapter(IBindingList bindingList)
    {
        this.bindingList = bindingList;
        DoFilter();
    }


    protected override void OnListChanged(ListChangedEventArgs e)
    {
        if (!filtering)
        {
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    bindingList.Insert(e.NewIndex, this[e.NewIndex]);
                    break;
            }
        }

        base.OnListChanged(e);
    }

    protected override void RemoveItem(int index)
    {
        if (!filtering)
        {
            bindingList.RemoveAt(index);
        }

        base.RemoveItem(index);
    }

    protected virtual void DoFilter()
    {
        filtering = true;
        this.Clear();

        foreach (T e in bindingList)
        {
            if (filter.Length == 0 || this.ISVisible(e))
            {
                this.Add((T)e);
            }
        }
        filtering = false;
    }

    protected virtual bool ISVisible(T element)
    {
        return true;
    }


    #region IBindingListView Members

    public void ApplySort(ListSortDescriptionCollection sorts)
    {
        throw new NotImplementedException();
    }

    public string Filter
    {
        get
        {
            return filter;
        }
        set
        {
            filter = value;
            DoFilter();
        }
    }

    public void RemoveFilter()
    {
        Filter = String.Empty;
    }

    public ListSortDescriptionCollection SortDescriptions
    {
        get { throw new NotImplementedException(); }
    }

    public bool SupportsAdvancedSorting
    {
        get { return false; }
    }

    public bool SupportsFiltering
    {
        get { return true; }
    }

    #endregion
}
Tarion
Add [Serializable] if you want to support Serialization :)
Tarion