2015-10-30 46 views
-1

我在3個不同的更新面板中有3個gridview,我在RowCommand之後更新它們。在調用數據綁定後Gridview沒有更新

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(); 
} 
+0

獲得價值請添加更多的代碼,包括'GridView1_RowCommand'。 –

+0

根據你發佈的內容很難確定你所看到或看不到的內容。請編輯此問題併發布所有相關的代碼,方法和事件。 – MethodMan

+0

我更新了代碼。兩個使用相同的更新方法,但只有'GridView1_RowCommand'可以更新所有3個gridview。 – Wen21

回答

0

YESgridview1_rowcommand將更新所有三個,因爲如果你看到gridview1_rowcommand。在該方法中,你還呼籲其他兩種GridView的的數據綁定。所以現在爲什麼只有在調用gridview1_rowcommand時纔會發生這種情況,因爲您可能正在調用第一個網格行命令或其他一些問題。

所以大概確定您是否在Response.write(id)