【问题标题】:Filtering datagridview by textbox change通过文本框更改过滤datagridview
【发布时间】:2012-07-25 05:05:20
【问题描述】:

通过 WinForms 中的 bindingsource 控制使用 Linq to sql,我无法让它工作:

private void textBox1_TextChanged(object sender, EventArgs e)
{
            productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'", textBox1.Text);
            MessageBox.Show("Changed");
}
        NorthwindDataContext dc;
        private void FrmFilter_Load(object sender, EventArgs e)
    {
        // create new data context
        dc = new NorthwindDataContext();

        // set the binding source data source to the full order table
        var qry = (from p in dc.Products select p).ToList();
        this.productBindingSource.DataSource = dc.GetTable<Product>();
    }

当我在文本框中键入一些字母时,datagridview 中没有任何反应。 感谢您的建议...

【问题讨论】:

  • 您的数据源极不可能有 dataGridViewTextBoxColumn2 列,看起来它属于 DataGridView 列。尝试对您的 datagridview 正在使用的数据源的列进行过滤。
  • 试过这个:this.productBindingSource.Filter = string.Format("ProductName like '%{0}%'", textBox1.Text.Trim().Replace( "'", "''"));没有任何改变。

标签: c# winforms linq-to-sql filtering


【解决方案1】:

尝试将您的代码更改为如下所示:

NorthwindDataContext dc;

private void FrmFilter_Load(object sender, EventArgs e)
{
  dc = new NorthwindDataContext();
  this.productBindingSource.DataSource = dc.GetTable<Product>();
  productDataGridView.DataSource = productBindingSource;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
  this.productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'",
                                     textBox1.Text);
}

确保您的 TextChanged 事件已连接并实际运行。另外,我从示例中删除了 qry,因为您没有在发布的代码中的任何地方使用它。


较早的编辑:

您不必在网格上重置 DataSource。

尝试将其更改为:

private void textBox1_TextChanged(object sender, EventArgs e) {
  if (textBox1.Text == string.Empty) {
    productBindingSource.RemoveFilter();
  } else {
    productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'", _
                                  textBox1.Text);
  }
}

目前我会避免担心替换那些特殊字符。先让过滤器工作。


这是一个工作示例,表单上只有一个 DataGridView 和一个 TextBox:

private DataTable dt = new DataTable("Test");
private BindingSource bs;

public Form1() {
  InitializeComponent();

  dt.Columns.Add("ProductName", typeof(string));

  DataRow dr1 = dt.NewRow();
  dr1["ProductName"] = "One A";
  dt.Rows.Add(dr1);

  DataRow dr2 = dt.NewRow();
  dr2["ProductName"] = "One B";
  dt.Rows.Add(dr2);

  DataRow dr3 = dt.NewRow();
  dr3["ProductName"] = "Two A";
  dt.Rows.Add(dr3);

  DataRow dr4 = dt.NewRow();
  dr4["ProductName"] = "Two B";
  dt.Rows.Add(dr4);

  bs = new BindingSource(dt, null);
  dataGridView1.DataSource = bs;
}

private void textBox1_TextChanged(object sender, EventArgs e) {
  if (textBox1.Text == string.Empty) {
    bs.RemoveFilter();
  } else {
    bs.Filter = string.Format("ProductName LIKE '*{0}*'", textBox1.Text);
  }
}

【讨论】:

  • 更新了正确的通配符。
  • 对不起,我试过这个没有成功,数据网格内容保持不变。
  • @AlphaBird 只是为了检查一下,但是您的 DataGridView 是否已经使用 productBindingSource 作为 DataSource 了?应该是的。
  • 是的,我的 DataGridView 已经使用 productBindingSource 作为它的 DataSource,当我加载表单时 datagridview 已满,谢谢你的努力 Lars ...请检查上面新添加的代码。
  • 对不起 Lars,我试过你的例子,我可以填充数据网格但不能过滤行,因为你说它是一个工作示例,所以出了点问题。
【解决方案2】:

这项工作对我来说很好!

this.productBindingSource.Filter = null;

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多