![](/img/trans.png)
[英]How to delete the specific row from gridview.Using builtin delete command button of gridview
[英]GridView Command Button Unable to Delete
我在GridView中添加了一個Delete Command按鈕,但是每當我按下按鈕時,我都會得到一個錯誤:
但是我不知道我應該使用什么事件以及刪除和編輯什么,這是我的代碼:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="lkDelte" runat="server" OnClientClick="return confirm('Are you sure you want to delete?')"
CommandName="Delete" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'></asp:Button>
</ItemTemplate>
</asp:TemplateField>
protected void gdCourse_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string con_str = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(con_str);
SqlCommand com = new SqlCommand("dbo.delete_documents", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter pDocument_ID = new SqlParameter("@document_id", row);
com.Parameters.Add(pDocument_ID);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
else
{
Label2.Text = "Unable to delete";
}
}
您的代碼沒什么錯,但是您忘記了處理網格視圖的RowDeleting
事件。
您需要處理gridview RowDeleting
方法:
protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
在這種情況下,您可以重新綁定此方法或將該方法保留為空,但添加它的簽名,以便事件可以正確觸發。
將此添加到您的程序:
protected void NameOfYourGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//bind your grid view here...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.