本文介绍了当DataSource是BindingList时过滤BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从excel工作表中阅读并为BindingList编写了此代码,在Form_Load中将其设置为数据源作为BindingSource:

I have read from a excel sheet and wrote this for a BindingList, in Form_Load this is set to a DataSource as BindingSource:

bd = new BindingSource(); //instance of BindingSource
bd.DataSource = ExcelOPS.LerExcel(); //LerExcel() method return a BindingList<T>

gvFiltro.DataSource = bd; //set a DataGridView named gvFiltro DataSource property
bindNav.BindingSource = bd; //set a BindingNavigator source

此功能正常!
我打算为该DataGridView gvFiltro创建一个组合框作为过滤器,因此在combobox的SelectedIndexChanged事件中,我尝试这样做:

This work fine!I intent to create a combobox as filter for this DataGridView gvFiltro, so in SelectedIndexChanged event of combobox, I try this:

this.gvFiltro.DataSource = null;
bd.Filter = string.Format("TAG_FAZENDA like '%{0}%'", cbTagFaz.Text);
gvFiltro.DataSource = bd;
gvFiltro.Update();
gvFiltro.Refresh();

bindNav.BindingSource = bd;
bindNav.Update();
bindNav.Refresh();

但DataGridView不变。我错过了什么?

But the DataGridView don't change. I missed something?

推荐答案

您不能使用属性进行过滤 BindingSource DataSource 设置为 BindingList< T>

You can not use Filter property to filter a BindingSource which it's DataSource is set to a BindingList<T>.

您可以使用Linq过滤 BindingList< T>

You can filter the BindingList<T> using Linq:

var filteredBindingList= new BindingList<T>(bindingList.Where(x=>some criteria).ToList());

然后,您可以使用过滤的绑定列表作为数据源。

Then you can use filtered binding list as data source.

这篇关于当DataSource是BindingList时过滤BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:11