调用数据绑定后,Gridview不会更新

时间:2015-10-30 01:48:39

标签: c# asp.net

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

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

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

提前致谢。

以下是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();
}

1 个答案:

答案 0 :(得分:0)

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

很可能验证您是否在Response.write(id)

中获得了价值