代码如下:
DataSet ds=new DataSet();
SqlDataAdapter ada=new SqlDataAdapter("select * from test","database=secure;uid=sa;pwd=;");
SqlCommandBuilder cb=new SqlCommandBuilder(ada);
ada.Fill(ds,"table0");
this.textBox1.DataBindings.Add("Text",ds.Tables["table0"],"test");
this.BindingContext[ds.Tables["table0"]].Position=0;
private void button3_Click(object sender, System.EventArgs e)
{
int position=this.BindingContext[ds.Tables["table0"]].Position;
ds.Tables["table0"].Rows[position][1]=textBox1.Text ;
ada.Update(ds.Tables["table0"])
}
private void button1_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds.Tables["table0"]].Position++;
}
我的问题是,当我更改了textBox1.text值,然后点击button3发现数据库的值没有改变。我又点击了button1,这时position的值+1,然后点击button3,发现刚才position对应的数据库行的值改变了。我个人认为如果控件绑定后,当前rows的值不立即更新。但我希望能立刻改变数据库的数据。有什么好的办法吗?或者我的观点有误。请指正。
谢谢.
DataSet ds=new DataSet();
SqlDataAdapter ada=new SqlDataAdapter("select * from test","database=secure;uid=sa;pwd=;");
SqlCommandBuilder cb=new SqlCommandBuilder(ada);
ada.Fill(ds,"table0");
this.textBox1.DataBindings.Add("Text",ds.Tables["table0"],"test");
this.BindingContext[ds.Tables["table0"]].Position=0;
private void button3_Click(object sender, System.EventArgs e)
{
int position=this.BindingContext[ds.Tables["table0"]].Position;
ds.Tables["table0"].Rows[position][1]=textBox1.Text ;
ada.Update(ds.Tables["table0"])
}
private void button1_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds.Tables["table0"]].Position++;
}
我的问题是,当我更改了textBox1.text值,然后点击button3发现数据库的值没有改变。我又点击了button1,这时position的值+1,然后点击button3,发现刚才position对应的数据库行的值改变了。我个人认为如果控件绑定后,当前rows的值不立即更新。但我希望能立刻改变数据库的数据。有什么好的办法吗?或者我的观点有误。请指正。
谢谢.
{
this.BindingContext[(ds.Tables["table0"]].].EndCurrentEdit();
ada.Update(ds.Tables["table0"])
}