2

多くのユーザーを持つデータソース (BindingList) がありますが、DataGridView に表示したくないユーザーがいます。それらを隠すことは可能ですか?機能するイベントが見つかりません。

RowsAdded は時々磨耗した行を非表示にします。

4

2 に答える 2

3

独自のフィルターを実装する必要があるようです。BindingList のフィルタリングされたバージョンを表示できる BindingList 用のアダプターを作成します。そこから継承するしかありません。これが私の例です。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;
    }
}

新しい List を DatagridView にバインドする方法は次のとおりです。

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

dataGridViewUser.DataSource = userSource;

さて、Filter プロパティはまだ役に立ちません。しかし、Adapter クラスはまだ非常に実験的なものです。しかし、DataGrid を使用した簡単な追加と削除では、うまくいくようです。

アダプターのコードは次のとおりです。

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
}
于 2009-09-22T01:28:02.063 に答える
2

プロパティで行をフィルタリングできBindingSource.Filterます。ただし、 の組み込み実装はBindingList<T>フィルタリングをサポートしていないため、自分で実装する必要があります。Google でいくつかの例を見つけることができます。これは面白そうです...

于 2009-09-21T23:06:47.460 に答える