Domanda

Ho un datasource (BindingList) con molti utenti, ma ci sono alcuni utenti che non desidera visualizzare nella mia DataGridView. E 'possibile nasconderli. Non riesco a trovare gli eventi che funziona.

RowsAdded a volte nasconde le righe worng.

È stato utile?

Soluzione

Sembra che devo realizzare il mio proprio filtro. I costruire un adattatore per wich BindingList in grado di visualizzare una versione filtrata di qualsiasi BindingList. Si deve solo inherite da esso. Qui è il mio esempio. Voglio solo gli utenti con user.CanEdit per mostrare = true

public class AhpUserFilter : FilterBindingListAdapter<AhpUser>
{

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

    }

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

Ecco come è possibile associare il nuovo elenco a un DataGridView:

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

dataGridViewUser.DataSource = userSource;

Ok, la proprietà Filter è inutile, ancora. Ma la classe adattatore è ancora molto sperimentale. Ma per la semplice aggiunta e la rimozione con un DataGrid sembra funzionare bene.

Ecco il codice per l'adattatore:

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
}

Altri suggerimenti

È possibile filtrare le righe con la proprietà BindingSource.Filter. Tuttavia, l'implementazione built-in di BindingList<T> non supporta il filtro, quindi è necessario implementare da soli. Si possono trovare alcuni esempi su Google. Questo sembra interessante ...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top