質問

私は多くのユーザーとデータソース(するBindingListを)持っていますが、私は私のDataGridViewに表示したくない一部のユーザーがあります。それらを非表示にすることが可能です。私は作品のイベントを見つけることができません。

時々worng行が非表示になりますRowsAddedます。

役に立ちましたか?

解決

私は自分のフィルタを実装するために持っているように、

見えます。私はするBindingListのフィルタされたバージョンを表示することができますするBindingListのウィッヒ用のアダプタを構築します。あなたはそれからinheriteする必要があります。ここに私の例です。私はuser.CanEditを持つユーザーのみを表示したい=真

public class AhpUserFilter : FilterBindingListAdapter<AhpUser>
{

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

    }

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

ここでは、DataGridViewの新しいリストをバインドする方法です。

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

dataGridViewUser.DataSource = userSource;

さて、Filterプロパティは、まだ、無用です。しかし、アダプタクラスは、まだ非常に実験的です。しかし、単純なデータグリッドでの追加と削除のためにそれがうまく動作しているようです。

ここでアダプタのコードがあります:

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
}

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top