开发者

How to filter DataGridView rows using a predicate?

开发者 https://www.devze.com 2023-01-06 04:21 出处:网络
I have a List<T> which is bound to a DataGridView like this: BindingSource bs = new BindingSource();

I have a List<T> which is bound to a DataGridView like this:

BindingSource bs = new BindingSource();
bs.DataSource = list;
myDataGridView.DataSource = bs;

开发者_JS百科I want to filter the rows that are displayed using a predicate. How do I achieve this?

Many thanks


The simple solution be following:

Func<T, bool> predicate = ...; // Func<T, bool> or Predicate<T>

BindingSource bs = new BindingSource();
bs.DataSource = list.Where(x => predicate(x));
myDataGridView.DataSource = bs;

If your predicate changes then either refresh the data source or just re-assign it using the new predicate.

0

精彩评论

暂无评论...
验证码 换一张
取 消