2009-09-21 17 views
2

多くのユーザーでデータソース(BindingList)を使用していますが、DataGridViewに表示したくないユーザーがいます。それらを隠すことは可能ですか?私は働く出来事を見つけることができません。C#DataSourceでDataGridViewの行を非表示にする方法

行を追加すると、行が不安定になることがあります。

答えて

3

私は自分のフィルタを実装する必要があります。 BindingListのフィルタリングされたバージョンを表示できるBindingList用のアダプタを作成します。あなたはただそれから継承しなければなりません。ここに私の例があります。

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

dataGridViewUser.DataSource = userSource; 

さて、Filterプロパティはまだ、無用である:私はあなたのDataGridViewに新しいリストをバインドする方法である。ここ=真

public class AhpUserFilter : FilterBindingListAdapter<AhpUser> 
{ 

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

    } 

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

user.CanEditを持つユーザーのみを表示したいです。しかし、アダプタクラスはまだ非常に実験的です。しかし、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 
} 
+0

あなたはシリアル化をサポートしたい場合は、[Serializableを]を追加:) – Tarion

+0

この例で、私は私のアプリで重要な機能を実装して助けました。共有していただきありがとうございます。とてもシンプルに聞こえるものを知っていた人は、とても複雑なのでしょうか? –

2

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

関連する問題