1

BindingSource在 LINQ to SQL 上使用,并BindingList在我的项目中实现了 a ,我必须使用 aTextbox来过滤 a 中的行DataGridView,所以当我删除文本框内容时,应该将 Filter 重置为空。

我的代码如下:

if (textBox1.Text.Length == 0)
{
    productBindingSource.Filter = null;
}
else
{
    productBindingSource.Filter = "ProductName = '" + textBox1.Text +"'";
    //productBindingSource.RemoveFilter();
}
productDataGridView.DataSource = productBindingSource;

但这无济于事,有什么想法吗?

4

4 回答 4

3

试试这样:

if (textBox1.Text.Length == 0) {
  productBindingSource.RemoveFilter();
} else {
  productBindingSource.Filter = "ProductName = '" + textBox1.Text +"'";
}

// productDataGridView.DataSource = productBindingSource;

如果 DataGridView 已经在使用 productBindingSource,则不需要再次对其进行 DataSourced。

于 2012-08-01T21:39:45.207 回答
2

http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

如图所示,这bindingsource.Filter是一个字符串值。默认值为空,所以只需执行以下操作:

productBindingSource.Filter = null;

尽管您可能必须做一些事情来更新您的 UI,但通常 bindingSource 会自行处理。

于 2012-08-01T18:38:31.387 回答
0

I found that "Find" method cannot be used directly with BindingList, but fortunately there is an alternative, using IEnumerable. After Implementing a BindingList in the project, I can filter a bound datagridview using the next code:

    private void button1_Click(object sender, EventArgs e)
    {
        var qry = (from p in dc.Products
                   select p).ToList();
        BindingList<Product> list = new BindingList<Product>(qry);
        IEnumerable<Product> selection = list.Where(m => m.ProductName.Contains(textBox1.Text) == true);
        productBindingSource.DataSource = selection;
    }
于 2012-08-02T01:05:46.870 回答
0

我假设您在 TextChanged 事件中测试文本框是否为空。当文本长度 = 0 时,可能没有调用您的方法。我不记得确切原因,但我以前经历过这种情况。

如果您使用的是您编写的 BindingList,请提供代码。RemoveFilter,将 Filter 设置为 null 或空字符串一直对我有用。

于 2014-05-14T13:02:58.607 回答