Home > Forum > ASP.NET > Delete Row from GridView

Delete Row from GridView

by nadeem157   on Nov 25, 2014   Category: ASP.NET  |  Views: 1391    |  Points: 5   |  Starter 

I want to delete the records from the Gridview on the click of ImageButton. Please see the ImageButton Code.

I tried with the below code, but it was not working, please help:-

aspx code
<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" AutoGenerateColumns="False" OnDataBound="grdCSRPageData_DataBound" AllowPaging="True" OnPageIndexChanging="grdCSRPageData_PageIndexChanging" DataKeyNames="Id" OnRowDeleting="grdCSRPageData_RowDeleting">
<AlternatingRowStyle BackColor="#CCCCCC" />
<%-- <asp:TemplateField ItemStyle-Width="30">
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" />
<asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" />
<asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<%-- <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />--%>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/images/edit.png" Width="15" Height="15" />
<%-- <span onclick="return confirm('Are you sure want to delete?')"> -- %>
<%--<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" ></asp:LinkButton>--%>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" />
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />

CS code:-

protected void grdCSRPageData_RowDeleting(object sender, GridViewDeleteEventArgs e)
bool IsDeleted = false;
//getting key value, row id
int Id = Convert.ToInt32(grdCSRPageData.DataKeys[e.RowIndex].Value.ToString());
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "DELETE FROM tbl_Pages WHERE Id=@ID";
cmd.Parameters.AddWithValue("@ID", Id);
cmd.Connection = conn;
IsDeleted = cmd.ExecuteNonQuery() > 0;
if (IsDeleted)
//record has been deleted successfully!
//call here gridview bind method and replace it..
Response.Write("Succesfully deleted");
//Error while deleting record
Response.Write("Some error");

Just page refreshes and nothing happens

Bookmark and Share:
  User Reply  | Ask a question  |   Reply 
  Re :Delete Row from GridView   
by Santosh Kumar Bind
on Jul 22 2015 11:37PM
Points : 10
Use This Code

cmd.CommandText = "DELETE FROM tbl_Pages WHERE ID="+Id;

cmd.Connection = conn;
IsDeleted = cmd.ExecuteNonQuery() > 0;

we can pass wrong @ID Parameter

Recent Post

Latest Posts