我现在用BindingSource绑定数据代码如下:
DataSet ds;
SqlCommandBuilder scb;
BindingSource b;
private void Form1_Load(object sender, EventArgs e)
{
textBox2.DataBindings.Clear();
comboBox1.DataBindings.Clear();
comboBox2.DataBindings.Clear();
comboBox3.DataBindings.Clear();
textBox5.DataBindings.Clear();
SqlConnection sql = new SqlConnection(dl.sqlstr);
SqlCommand sqlc = new SqlCommand();
sqlc.Connection = sql;
sqlc.CommandText = "select * from gh";
ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlc);
sda.Fill(ds, "t1");
b = new BindingSource(ds, "t1");
dataGridView1.DataSource = b;
sql.Close();
////////////////////////////////
textBox2.DataBindings.Add("Text", ds, "t1.xm");
comboBox1.DataBindings.Add("text", ds, "t1.ys");
comboBox2.DataBindings.Add("text", ds, "t1.ks");
comboBox3.DataBindings.Add("text", ds, "t1.ghlb");
textBox5.DataBindings.Add("text", ds, "t1.blbs");
}
上面这段代码是我将所有的textbox和combobox都绑定了。我现在在‘新增’按钮中点击时想新增加一行:
private void button1_Click(object sender, EventArgs e)
{
b.AddNew();
}
我在所有的textbox和combobox控件焦点离开事件中都写了如下代码:
private void textBox2_Leave(object sender, EventArgs e)
{
b.ResetBindings(false); }
现在发现的问题是:点击‘新增’按钮后datagridview中是增加了一行,但是我在其中的一个textbox中输入一些字母后,发现datagridview的第一行对应列并没有自动更新,但是又增加一行(第2行)时,这时在textbox中输入字母后,datagridview对应列可以跟着变化。请问是什么原因造成的,应该如何解决!请用代码相告!这是第一个问题,第二个问题是:我添加第二、三......行后,操作textbox控件,发现无论怎么变化textbox的值都更改的是datagridview的第一行数据,尽管点添加后datagridview左侧的箭头已经指向第2行了,但是操作的还是一行。这个又应该如何解决!也请代码相告!
DataSet ds;
SqlCommandBuilder scb;
BindingSource b;
private void Form1_Load(object sender, EventArgs e)
{
textBox2.DataBindings.Clear();
comboBox1.DataBindings.Clear();
comboBox2.DataBindings.Clear();
comboBox3.DataBindings.Clear();
textBox5.DataBindings.Clear();
SqlConnection sql = new SqlConnection(dl.sqlstr);
SqlCommand sqlc = new SqlCommand();
sqlc.Connection = sql;
sqlc.CommandText = "select * from gh";
ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlc);
sda.Fill(ds, "t1");
b = new BindingSource(ds, "t1");
dataGridView1.DataSource = b;
sql.Close();
////////////////////////////////
textBox2.DataBindings.Add("Text", ds, "t1.xm");
comboBox1.DataBindings.Add("text", ds, "t1.ys");
comboBox2.DataBindings.Add("text", ds, "t1.ks");
comboBox3.DataBindings.Add("text", ds, "t1.ghlb");
textBox5.DataBindings.Add("text", ds, "t1.blbs");
}
上面这段代码是我将所有的textbox和combobox都绑定了。我现在在‘新增’按钮中点击时想新增加一行:
private void button1_Click(object sender, EventArgs e)
{
b.AddNew();
}
我在所有的textbox和combobox控件焦点离开事件中都写了如下代码:
private void textBox2_Leave(object sender, EventArgs e)
{
b.ResetBindings(false); }
现在发现的问题是:点击‘新增’按钮后datagridview中是增加了一行,但是我在其中的一个textbox中输入一些字母后,发现datagridview的第一行对应列并没有自动更新,但是又增加一行(第2行)时,这时在textbox中输入字母后,datagridview对应列可以跟着变化。请问是什么原因造成的,应该如何解决!请用代码相告!这是第一个问题,第二个问题是:我添加第二、三......行后,操作textbox控件,发现无论怎么变化textbox的值都更改的是datagridview的第一行数据,尽管点添加后datagridview左侧的箭头已经指向第2行了,但是操作的还是一行。这个又应该如何解决!也请代码相告!
comboBox1.DataBindings.Add("text", b, "ys");
comboBox2.DataBindings.Add("text", b, "ks");
comboBox3.DataBindings.Add("text", b, "ghlb");
textBox5.DataBindings.Add("text", b, "blbs"); 编辑器默认在焦点离开编辑框后提交数据
{
// b.ResetBindings(false); 这个不要 }
例如说我在textbox1中输入完某字母后,不能实时更新datagridview中对应列了,得鼠标点回来双击后才显示出来,是怎么回事,请指教!
bindingsource bs=new bindingsource();
bs.datasource=数据名称(一般为集合 及 DataSet)
控件名称.datasource=bs;