开发者

How to filter DataGridView rows using a predicate?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜