如何在C#中创建datagridview绑定到BindingSource的搜索文本框?

如何在C#中创建datagridview绑定到BindingSource的搜索文本框?,c#,datagridview,bindingsource,C#,Datagridview,Bindingsource,我想在C#和SQLServer中的数据网格视图上创建一个搜索文本框,但datagridview绑定到BindingSource。 我尝试了这段代码没有功能 private void ucObat_Load(object sender, EventArgs e) { db = new DbSIMP3Entities(); tbObatBindingSource.DataSource = db.tbObats.ToList();

我想在C#和SQLServer中的数据网格视图上创建一个搜索文本框,但datagridview绑定到BindingSource。 我尝试了这段代码没有功能

private void ucObat_Load(object sender, EventArgs e)
        {
            db = new DbSIMP3Entities();
            tbObatBindingSource.DataSource = db.tbObats.ToList();
            tbSatuanBindingSource.DataSource = db.tbSatuans.ToList();
            tbKategoriBindingSource.DataSource = db.tbKategoris.ToList();
            tbMerkBindingSource.DataSource = db.tbMerks.ToList();

        }
private void txtSearch_Click(object sender, EventArgs e)
        {
            tbObatBindingSource.Filter = "NmObat like '&" + txtSearch.Text + "&'";
        }
仅当数据源实现
IBindingListView

只有实现IBindingListView接口的基础列表支持筛选

因此,要使其工作,您必须更改基础数据源

如果出于某种原因不想使用
DataView
,请尝试使用
BindingSource
本身作为数据源

private BindingSource bs = new BindingSource();
private BindingList<ObatsEntity> initialObats = new BindingList<ObatsEntity>();

private void ucObat_Load(object sender, EventArgs e)
{
    db = new DbSIMP3Entities();
    initialObats = new BindingList<ObatsEntity>( db.tbObats.ToList() );
    bs.DataSource = initialObats;
    dataGridView.DataSource = bs;
}

private void txtSearch_Click(object sender, EventArgs e)
{
    var filteredObats = new BindingList<ObatsEntity>( initialObats.Where( o => o.NmObat == txtSearch.Text ).ToList() );
    bs.DataSource = filteredObats;
    bs.ResetBindings();
}
private BindingSource bs=new BindingSource();
private BindingList initialObats=new BindingList();
私有无效ucObat_加载(对象发送方,事件参数e)
{
db=新的DbSIMP3Entities();
initialObats=新绑定列表(db.tbObats.ToList());
bs.DataSource=initialObats;
dataGridView.DataSource=bs;
}
私有void txtSearch_单击(对象发送方,事件参数e)
{
var filteredObats=newbindingslist(initialObats.Where(o=>o.NmObat==txtSearch.Text).ToList());
bs.DataSource=过滤数据;
ResetBindings();
}

如果过滤器或搜索为“like txtSearch.Text”。代码是什么?这是足够的操作:
initialObats.Where(o=>o.NmObat==txtSearch.Text).ToList()
-它将只返回满足条件的项目
o.NmObat==txtSearch.Text
=