关于如何编辑绑定后的TextBox来完成对数据源的更新
2007-06-30 00:11
330 查看
问题:
窗口中控件绑定到dataSet1上,但调用this.BindingContext[this.dataSet1].EndCurrentEdit();
程序并没有将当前的最新编辑存入dataSet1,
解决方法:
MSDN中说明了BindingContext的索引访问总是会返回一个BindingManagerBase,所以
即使你传入了一个错误的参数,例如
this.DataSet1也会有返回值,所以造成错误的认为调用是正确的。
DataSet的绑定实际上内部是使用DataView实现的,所以有俩种解决方法:
1、将控件绑定到DataView,而DataView关联DataSet,结束编辑时调用:
BindingContext[this.dataview1].EndCurrentEdit();
但这种方法如果有俩个表或更多调用就麻烦一些。
2、直接寻找所有的绑定环境,并结束编辑。
BindingManagerBase bm;
foreach (System.Collections.DictionaryEntry item in
this.BindingContext) {
bm = (item.Value as System.WeakReference).Target as
BindingManagerBase;
if (bm != null) {
bm.EndCurrentEdit();
}
}
以下是可以成功更新数据库的一组代码:
全局变量#region 全局变量
SqlConnection conn=new SqlConnection();
SqlCommand com=new SqlCommand();
SqlDataAdapter da=null;
DataSet ds=new DataSet();
#endregion
private void Form1_Load(object sender, System.EventArgs e)
...{
conn.ConnectionString="Server=.;Trusted_connection=true;DataBase=Northwind";
com=conn.CreateCommand();
com.CommandText="select * from Region";
da=new SqlDataAdapter(com);
SqlCommandBuilder comb=new SqlCommandBuilder(da);
da.Fill(ds,"region");
this.DBind();
this.textBox1.DataBindings.Add("Text",ds.Tables["region"],"RegionID");
this.textBox2.DataBindings.Add("Text",ds.Tables["region"],"RegionDescription");
}
private void button1_Click(object sender, System.EventArgs e)
...{
//这一句是为了结束所有被绑定的控件结束编辑状态,可以写如下的
//第1种:
BindingManagerBase myBind=this.BindingContext[this.ds.Tables["region"]];
myBind.EndCurrentEdit();
//第2种:
//this.BindingContext[this.ds.Tables["region"]];
//以上两种编辑方法都可以结束控件
da.Update(ds,"region");
this.DBind();
}
private void DBind()
...{
this.dataGrid1.DataSource=ds.Tables["region"];
}
窗口中控件绑定到dataSet1上,但调用this.BindingContext[this.dataSet1].EndCurrentEdit();
程序并没有将当前的最新编辑存入dataSet1,
解决方法:
MSDN中说明了BindingContext的索引访问总是会返回一个BindingManagerBase,所以
即使你传入了一个错误的参数,例如
this.DataSet1也会有返回值,所以造成错误的认为调用是正确的。
DataSet的绑定实际上内部是使用DataView实现的,所以有俩种解决方法:
1、将控件绑定到DataView,而DataView关联DataSet,结束编辑时调用:
BindingContext[this.dataview1].EndCurrentEdit();
但这种方法如果有俩个表或更多调用就麻烦一些。
2、直接寻找所有的绑定环境,并结束编辑。
BindingManagerBase bm;
foreach (System.Collections.DictionaryEntry item in
this.BindingContext) {
bm = (item.Value as System.WeakReference).Target as
BindingManagerBase;
if (bm != null) {
bm.EndCurrentEdit();
}
}
以下是可以成功更新数据库的一组代码:
全局变量#region 全局变量
SqlConnection conn=new SqlConnection();
SqlCommand com=new SqlCommand();
SqlDataAdapter da=null;
DataSet ds=new DataSet();
#endregion
private void Form1_Load(object sender, System.EventArgs e)
...{
conn.ConnectionString="Server=.;Trusted_connection=true;DataBase=Northwind";
com=conn.CreateCommand();
com.CommandText="select * from Region";
da=new SqlDataAdapter(com);
SqlCommandBuilder comb=new SqlCommandBuilder(da);
da.Fill(ds,"region");
this.DBind();
this.textBox1.DataBindings.Add("Text",ds.Tables["region"],"RegionID");
this.textBox2.DataBindings.Add("Text",ds.Tables["region"],"RegionDescription");
}
private void button1_Click(object sender, System.EventArgs e)
...{
//这一句是为了结束所有被绑定的控件结束编辑状态,可以写如下的
//第1种:
BindingManagerBase myBind=this.BindingContext[this.ds.Tables["region"]];
myBind.EndCurrentEdit();
//第2种:
//this.BindingContext[this.ds.Tables["region"]];
//以上两种编辑方法都可以结束控件
da.Update(ds,"region");
this.DBind();
}
private void DBind()
...{
this.dataGrid1.DataSource=ds.Tables["region"];
}
相关文章推荐
- 关于在插入的模版中如何为已绑定了Text属性的TextBox设置默认值
- winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值?
- C#中,datagridview与sql数据源绑定后,通过控件更改数据如何自动回写更新到数据源
- 关于模板控件如何实现多数据源绑定的问题
- 黑马程序员—关于winform中如何令textbox随输入的文本内容的长度而实时改变宽度
- 编辑绑定数据源后GridView的某一列
- WPF 自带Datagrid编辑后无法更新数据源的问题
- 如何更新地图发布中间件的WGS84经纬度影像数据源
- 关于绑定变量关闭的情况,Oracle是如何工作的?
- 关于工作流引擎,工作流程管理系统,ccflow的更新,如何去更新?
- 关于程序本身启动更新程序完成更新后再启动自己的源码
- Gridview的初步应用 删除/更新/撤销/编辑/绑定/翻页
- 关于如何取消匿名函数的绑定以及arguments.callee的用法
- winform datagridview 不显示默认第一列 不显示未绑定列 数据源发生改变时自动更新 (转)
- 关于如何绑定Jquery 的scroll事件(兼容浏览器 Wookmark瀑布流插件)
- combobox和textBox的数据源绑定项
- 如何完成域名和ip地址的绑定
- 关于iOS UITableView 数据源数组加载完成之后,滑动时出现cell为空的问题解决笔记
- 如何让GridView控件在绑定的数据源没有数据时也显示标题
- 关于open.window子页面执行完成后如何刷新父页面的问题