关于如何编辑绑定后的TextBox来完成对数据源的更新

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ranlianjie/article/details/1672049

问题:
窗口中控件绑定到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();
                }
            }
以下是可以成功更新数据库的一组代码:

全局变量
        
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"];
        }
展开阅读全文

gridview 编辑完后更新不能获取更新后的值

08-26

问题如题,代码在下面rn[code=C#]rnprotected void Page_Load(object sender, EventArgs e)rn rn if (!Page.IsPostBack)rn rn tryrn rn DataTable taskTable = new DataTable("TaskItem");rnrn // Create the columns.rn taskTable.Columns.Add("图号", typeof(string));rn taskTable.Columns.Add("工序号", typeof(int));rn taskTable.Columns.Add("工序名称", typeof(string));rn taskTable.Columns.Add("简工艺", typeof(string));rn taskTable.Columns.Add("准备工时,分钟", typeof(float));rn taskTable.Columns.Add("单件工时,分钟", typeof(float));rn taskTable.Columns.Add("录入人", typeof(string));rn taskTable.Columns.Add("录入时间", typeof(string));rn taskTable.Columns.Add("审核人", typeof(string));rn taskTable.Columns.Add("审核时间", typeof(string));rnrn //Persist the table in the Session object.rn //Session["TaskTable"] = taskTable;rn Session.Add("TaskTable", taskTable);rn // 将表添加到DataView对象中rn DataView view = new DataView((DataTable)Session["TaskTable"]);rnrn // 指定GridView的数据源,并绑定数据rn TaskGridView.DataSource = view;rn TaskGridView.DataBind();rn rn catch (Exception ex) rn rn rn rn rn rn[/code]rn[code=C#]rnprotected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)rn rnrn if (Session["TaskTable"] != null)rn rn //Retrieve the table from the session object.rn DataTable dt = (DataTable)Session["TaskTable"];rnrn //Update the values.rn GridViewRow row = TaskGridView.Rows[e.RowIndex];rnrn dt.Rows[row.DataItemIndex]["图号"] = TaskGridView.Rows[e.RowIndex].Cells[1].Text;rn dt.Rows[row.DataItemIndex]["工序号"] = int.Parse(TaskGridView.Rows[e.RowIndex].Cells[2].Text);rn dt.Rows[row.DataItemIndex]["工序名称"] = ((TextBox)TaskGridView.Rows[e.RowIndex].Cells[3].Controls[0]).Text;rn dt.Rows[row.DataItemIndex]["简工艺"] = ((TextBox)TaskGridView.Rows[e.RowIndex].Cells[4].Controls[0]).Text;rn dt.Rows[row.DataItemIndex]["准备工时,分钟"] = ((TextBox)TaskGridView.Rows[e.RowIndex].Cells[5].Controls[0]).Text;rn dt.Rows[row.DataItemIndex]["单件工时,分钟"] = ((TextBox)TaskGridView.Rows[e.RowIndex].Cells[6].Controls[0]).Text;rn dt.Rows[row.DataItemIndex]["录入人"] = Request.ServerVariables["LOGON_USER"].ToString().Substring(4);rn dt.Rows[row.DataItemIndex]["录入时间"] = DateTime.Now;rnrn //Reset the edit index.rn TaskGridView.EditIndex = -1;rn rn //Bind data to the GridView control.rn BindData();rn rn elsern rn Response.Write("session is null");rn rn rn[/code]rn 论坛

没有更多推荐了,返回首页