我在行代码更新中得到错误,此代码数据无法更新其仅显示gridview中的旧数据。 [英] I get error in row updateing in this code data cannot be update its show only old data in gridview.

查看:57
本文介绍了我在行代码更新中得到错误,此代码数据无法更新其仅显示gridview中的旧数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    int BookingRefNo = int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
    string AssignedPriority = ((TextBox)row.Cells[11].Controls[0]).Text;
    string Status = ((TextBox)row.Cells[12].Controls[0]).Text;
    SqlConnection con1 = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("Update TSafetyMaster set AssignedPriority=@AssignedPriority,Status=@Status where BookingRefNo=@BookingRefNo", con1);
    cmd.Parameters.Add("@BookingRefNo", SqlDbType.Int).Value = BookingRefNo;
    cmd.Parameters.Add("@AssignedPriority", SqlDbType.VarChar).Value = AssignedPriority;
    cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = Status;
    con1.Open();
    cmd.ExecuteNonQuery();
    GridView2.EditIndex = -1;
    GridView2.DataBind();
}

推荐答案

您需要从数据库获取数据并在更新后再次绑定GridView2。

例如,在你的页面加载中你可能有类似下面的gridview绑定代码



you need to get the data from database and bind the GridView2 again after update.
for example, in your page load you may have gridview binding code something like below

if(!IsPagePostBack)
{
    BindGridview(); // get data from database and call GridView2.DataBind();
}





插入后调用相同



call the same after insert

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  // your current code goes here ...
  // after cmd.ExecuteNonQuery();
   BindGridview();
}


问题在于绑定Grid的方式。您必须在页面加载中检查 IsPostBack 并绑定数据。

否则,对于RowUpdating,它将首先进入页面加载并再次绑定网格。是的,你会得到旧的价值而不是新的价值。



所以,请在下面做...

The problem is with the way you are binding the Grid. You must check for IsPostBack in Page Load and bind the data.
Otherwise, for RowUpdating, it would go inside the Page Load first and bind the Grid again. Do, you will get the old values instead of new values.

So, do like below...
private void Page_Load()
{
    if (!IsPostBack)
    {
        // Call your Bind method here.
        BindYourGrid();
    }
}


这篇关于我在行代码更新中得到错误,此代码数据无法更新其仅显示gridview中的旧数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆