窗体上有一个textbox,bindingsource和一个datagridview。初始化时datagridview绑定一个表,当textbox改变时,datagridview即时更新。该怎么做?
我的做法如下:(textbox变化时,datagridview没有更新)
private void Form_Load(object sender, EventArgs e)
{
   SqlConnection con = sqlclass.SqlConBind();
   con.Open();
   SqlDataAdapter sda = new SqlDataAdapter("select ID,项目名称 from 项目名称表", con);
    sda.Fill(ds);
    con.Close();
    bindingSource1.DataSource = ds.Tables[0];
   this.dataGridView1.DataSource = bindingSource1;
}
 private void textBox1_TextChanged(object sender, EventArgs e)
 {
     SqlConnection con = sqlclass.SqlConBind();
     con.Open();
     SqlCommand scd1 = new SqlCommand("select * from 项目名称表 where 项目名称 like @项目名称 and 作废=@作废 order by ID", con);
     scd1.Parameters.Add("@项目名称", SqlDbType.Char).Value = textBox1.Text + "%";
      SqlDataAdapter sda = new SqlDataAdapter();
      sda.SelectCommand = scd1;
      sda.Fill(ds);
      con.Close();                          
      bindingSource1.DataSource = ds.Tables[0];               
      this.dataGridView1.Refresh();
      this.dataGridView1.DataSource = bindingSource1;
}

解决方案 »

  1.   

    this.dataGridView1.Refresh();
          this.dataGridView1.DataSource = bindingSource1;应该改为这样吧
    this.dataGridView1.DataSource = bindingSource1;
          this.dataGridView1.Refresh();
      

  2.   

    不行啊,执行textbox_TextChanged的时候,datagridview没有变化
      

  3.   

    To: 楼主
    需要页面整体刷新。页面不刷新是不会有结果的。除非你用的Ajax啊
      

  4.   

    一般,textbox的AutoPostBack是设为false的 因为一般不会将其用作页面提交控件。
    硬要使用的话,将AutoPostBack属性设为true看看
      

  5.   

    晕 错了 我理解为Web form了 请把偶的发言忽略。-_-!
      

  6.   

    bindingSource1.DataSource = ds.Tables[0];               
          this.dataGridView1.Refresh();
          this.dataGridView1.DataSource = bindingSource1;
    改成-------------------------------
    private DataSet _ds=null;  
    .....open db时,填充_ds;
    bindingSource.datasource=_ds;
    bindingSource.datamember="xxx"; //表名
    this.dataGridView1.DataSource = bindingSource1;-------------
    TextBox Change时用Merge方法。然后调用bindingsource1.ResetBindings(false);
    这样就可以了。楼主试试
      

  7.   

    当TextBox TextChanged触发时,把_ds清空。
    然后重新Merge新的内容进去。这个很重要 。
      

  8.   

    问题已经解决
    private void Form_Load(object sender, EventArgs e)
    {
       SqlConnection con = sqlclass.SqlConBind();
       con.Open();
       SqlDataAdapter sda = new SqlDataAdapter("select ID,项目名称 from 项目名称表", con);
        sda.Fill(ds);
        con.Close();
        bindingSource1.DataSource = ds.Tables[0];
       this.dataGridView1.DataSource = bindingSource1;
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
     {
         DataSet ds = new DataSet();// 清空
         SqlConnection con = sqlclass.SqlConBind();
         con.Open();
         SqlCommand scd1 = new SqlCommand("select * from 项目名称表 where 项目名称 like @项目名称  order by ID", con);
         scd1.Parameters.Add("@项目名称", SqlDbType.Char).Value = textBox1.Text + "%";
          SqlDataAdapter sda = new SqlDataAdapter();
          sda.SelectCommand = scd1;
          sda.Fill(ds);
          con.Close();  
          bindingSource1.ResetBindings(false);                        
          bindingSource1.DataSource = ds.Tables[0];    //重新邦定         
          this.dataGridView1.DataSource = bindingSource1;
    }