C # How to hide row in DataGridView using DataSource
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 to share