调用databind后,Gridview未更新

本文关键字:更新 Gridview databind 调用 | 更新日期: 2023-09-27 18:19:48

我在3个不同的更新面板中有3个网格视图,我在RowCommand之后更新它们。

GridView1.DataBind();
GridView2.DataBind();
GridView3.DataBind();

奇怪的是,只有GridView1_RowCommand可以更新所有三个网格视图。GridView2_RowCommand使用相同的方法,但我无法更新网格视图。请帮帮我。

提前谢谢。

以下是Rowcommand的代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //check the commandName
    if (e.CommandName != "SaveStartTime")
        return;
    int rowIndex = int.Parse(e.CommandArgument.ToString());
    string id = GridView1.Rows[rowIndex].Cells[0].Text;
    Label time = GridView1.Rows[rowIndex].Cells[4].FindControl("ActualTimeStart") as Label;
    time.Text = DateTime.Now.ToString("HH:mm");
    var Cn = new System.Data.SqlClient.SqlConnection();
    Cn.ConnectionString = "Server=.''SqlExpress;Database=CMOS;Trusted_Connection=True";
    Cn.Open();
    var Cm = Cn.CreateCommand();
    string store = string.Format(@"UPDATE [ApprovedExitPass] SET ActualTimeStart = '{0}' WHERE Id='{1}'", time.Text, id);
    SqlCommand cmd = new SqlCommand(store, Cn);
    cmd.Parameters.AddWithValue("@ActualTimeStart", time.Text);
    cmd.ExecuteNonQuery();
    Cn.Close();
    GridView1.DataBind();
    GridView2.DataBind();
    GridView3.DataBind();
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //check the commandName
    if (e.CommandName != "SaveReturnTime")
        return;
    int rowIndex = int.Parse(e.CommandArgument.ToString());
    string id = GridView2.Rows[rowIndex].Cells[0].Text;
    Response.Write(id);
    Label time = GridView2.Rows[rowIndex].Cells[4].FindControl("ActualTimeArrive") as Label;
    time.Text = DateTime.Now.ToString("HH:mm");
    var Cn = new System.Data.SqlClient.SqlConnection();
    Cn.ConnectionString = "Server=.''SqlExpress;Database=CMOS;Trusted_Connection=True";
    Cn.Open();
    var Cm = Cn.CreateCommand();
    string store = string.Format(@"UPDATE [ApprovedExitPass] SET ActualTimeArrive = '{0}' WHERE Id='{1}'", time.Text, id);
    SqlCommand cmd = new SqlCommand(store, Cn);
    cmd.Parameters.AddWithValue("@ActualTimeArrive", time.Text);
    cmd.ExecuteNonQuery();
    Cn.Close();
    GridView1.DataBind();
    GridView2.DataBind();
    GridView3.DataBind();
}

调用databind后,Gridview未更新

YESgridview1_rowcommand将更新所有三个,因为如果您在该方法中看到gridview1_rowcommand,您也将调用其他两个网格视图的数据绑定。所以现在为什么只在调用gridview1_rowcommand时发生这种情况,因为您可能正在调用第一个网格行命令或其他问题。

SO可能会验证您是否在Response.write(id) 中获得值