2015-10-30 128 views
0

這是刪除一行代碼:刪除的GridView從GridView中刪除的行數據直接

else if (e.CommandName == "Deleterow") 
       { 
        GridViewRow gr = (GridViewRow) 
((Button)e.CommandSource).NamingContainer;  
        SqlCommand com = new SqlCommand("StoredProcedure4", con); 
        com.CommandType = CommandType.StoredProcedure; 
        com.Parameters.AddWithValue("@ID", gr.Cells[0].Text); 
        com.ExecuteNonQuery(); 


       } 

ASPX:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     SqlConnection con = Connection.DBconnection(); 
     if (e.CommandName == "EditRow") 
     { 
      GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;    
      Textid.Text = gr.Cells[0].Text; 
      Textusername.Text = gr.Cells[1].Text; 
      Textclass.Text = gr.Cells[2].Text; 
      Textsection.Text = gr.Cells[3].Text; 
      Textaddress.Text = gr.Cells[4].Text; 
     } 
     else if (e.CommandName == "Deleterow") 
     { 
      GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;  
      SqlCommand com = new SqlCommand("StoredProcedure4", con); 
      com.CommandType = CommandType.StoredProcedure; 
      com.Parameters.AddWithValue("@ID", gr.Cells[0].Text); 
      com.ExecuteNonQuery(); 

     } 
    } 

當我刪除按鈕,它刪除數據行重新加載頁面後。

所以我需要直接從gridview刪除行。

對於該做什麼,任何人都可以幫忙嗎?

感謝,

+0

無需重新加載頁面只是重新綁定網格com.ExecuteNonQuery後(); –

+0

而不是使用'Response.Redirect()'重新綁定你的gridview – Prabhat

+0

@Rani:你的** Page_Load **內容。這對我們來說更容易給你適當的解決方案。 – Prabhat

回答

0
  else if (e.CommandName == "Deleterow") 
      { 
       GridViewRow gr = (GridViewRow) 
       ((Button)e.CommandSource).NamingContainer;  
       SqlCommand com = new SqlCommand("StoredProcedure4", con); 
       com.CommandType = CommandType.StoredProcedure; 
       com.Parameters.AddWithValue("@ID", gr.Cells[0].Text); 
       com.ExecuteNonQuery(); 
       //Response.Redirect("studententry.aspx"); Remove this line 
       //call method here you have used to populate the gridview at page load. 
      }         
+0

如何重新綁定網格? – pcs

+0

這條線在你的答案中,在這裏重新綁定網格。? – pcs

+1

您在頁面加載時用於填充gridview的方法調用方法 – Prabhat