C # How to hide row in DataGridView using DataSource

I have a datasource (BindingList) with many users, but there are some users that I don't want to be displayed in my DataGridView. Can I hide them. I cannot find events that work.

RowsAdded sometimes hides worng rows.

+2


source to share


2 answers


It looks like I need to implement my own filter. I am creating an adapter for a BindingList that can display a filtered version of any BindingList. You just need to inherit it. Here's my example. I only want to show 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;
    }
}

      

This is how you can bind a new list to the DatagridView:

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

dataGridViewUser.DataSource = userSource;

      



Okay, the Filter property is useless. But the adapter class is still very experimental. But for simple add and remove using DataGrid it works 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
}

      

+3


source


You can filter rows using a property BindingSource.Filter

. However, the built-in implementation BindingList<T>

does not support filtering, so you need to implement it yourself. You can find some examples on Google. This one looks interesting ...



+2


source







All Articles