I have a DataGridView that is displaying a List of objects (not DataSet). Is there an easy way to set a filter, so the DGV will only display rows that match the filter?
IList<T> log = ...;
dgv.DataSource = log;
I have a DataGridView that is displaying a List of objects (not DataSet). Is there an easy way to set a filter, so the DGV will only display rows that match the filter?
IList<T> log = ...;
dgv.DataSource = log;
You could do Log.Where(filter) That's generally how I filter items in a list bound to a DGV if I don't have control over the generation.
Do you have LINQ available? If so, one option is:
dgv.DataSource = log.Where(x=>x.IsActive && x.Type == 3).ToList();
However, new/removed rows won't update the original list (edits to existing rows are fine).
If not LINQ, you can do the same with List<T>.FindAll:
dgv.DataSource = log.FindAll(delegate (SomeType x) {
return x.IsActive && x.Type == 3;});
There is the IBindingListView.SupportsFiltering / IBindingListView.Filter pair, but none of the standard lists (including BindingList<T>) implement this feature.