C# 按文本框更改筛选datagridview

C# 按文本框更改筛选datagridview,c#,winforms,linq-to-sql,filtering,C#,Winforms,Linq To Sql,Filtering,通过WinForms中的bindingsource控件使用Linq到sql,我无法实现以下目标: private void textBox1_TextChanged(object sender, EventArgs e) { productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'", textBox1.Text); MessageBox.Show("Changed"

通过WinForms中的bindingsource控件使用Linq到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>();
    }
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
productBindingSource.Filter=string.Format(“类似于'*{0}*'”的产品名称,textBox1.Text);
MessageBox.Show(“已更改”);
}
NorthwindDataContext dc;
私有void FrmFilter\u加载(对象发送方,事件参数e)
{
//创建新的数据上下文
dc=新的NorthwindDataContext();
//将绑定源数据源设置为完整订单表
var qry=(从dc.Products中的p选择p.ToList();
this.productBindingSource.DataSource=dc.GetTable();
}
当我在文本框中键入一些字母时,datagridview中不会发生任何事情。
感谢您的建议…

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

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);
}
我现在不会担心替换那些特殊字符。首先让过滤器工作


下面是一个工作示例,表单上只有一个DataGridView和一个文本框:

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);
  }
}

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

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);
}
我现在不会担心替换那些特殊字符。首先让过滤器工作


下面是一个工作示例,表单上只有一个DataGridView和一个文本框:

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);
  }
}

这工作对我来说很好

this.productBindingSource.Filter = null;

这工作对我来说很好

this.productBindingSource.Filter = null;

您的数据源不太可能有dataGridViewTextBoxColumn2列,该列看起来属于DataGridView列。尝试对datagridview正在使用的数据源的列进行筛选。尝试了以下操作:this.productBindingSource.Filter=string.Format(“类似于“{0}%”的ProductName,textBox1.Text.Trim().Replace(“,”);没有更改。您的数据源不太可能有dataGridViewTextBoxColumn2列,该列看起来属于DataGridView列。尝试对datagridview正在使用的数据源的列进行筛选。尝试了以下操作:this.productBindingSource.Filter=string.Format(“类似于“{0}%”的ProductName,textBox1.Text.Trim().Replace(“,”);未更改。已使用适当的通配符进行更新。抱歉,我尝试了此操作但未成功,datagrid内容保持不变。@AlphaBird只是为了检查,但是您的DataGridView是否已将productBindingSource用作其数据源?应该是。是的,我的DataGridView已经使用productBindingSource作为它的数据源,当我加载表单DataGridView时,感谢您的努力。。。请检查上面新添加的代码。很抱歉,Lars,我尝试了你的示例,我可以填充datagrid,但不能过滤行,因为你说这是一个工作示例,所以出现了一些问题。使用正确的通配符进行了更新。很抱歉,我尝试了但没有成功,datagrid内容保持不变。@AlphaBird只是为了检查,但是您的DataGridView是否已经使用productBindingSource作为数据源了?应该是。是的,我的DataGridView已经使用productBindingSource作为它的数据源,当我加载表单DataGridView时,感谢您的努力。。。请检查上面新添加的代码。对不起,Lars,我尝试了你的示例,我可以填充datagrid,但不能过滤行,因为你说这是一个工作示例,所以出现了一些问题。