窗体上有一个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;
}
我的做法如下:(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;
}
this.dataGridView1.DataSource = bindingSource1;应该改为这样吧
this.dataGridView1.DataSource = bindingSource1;
this.dataGridView1.Refresh();
需要页面整体刷新。页面不刷新是不会有结果的。除非你用的Ajax啊
硬要使用的话,将AutoPostBack属性设为true看看
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);
这样就可以了。楼主试试
然后重新Merge新的内容进去。这个很重要 。
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;
}