Your .aspx page grid code.
<asp:templatefield headertext="Action" xmlns:asp="#unknown">
<itemtemplate>
<asp:linkbutton id="lblDelete" runat="server" commandname="DeleteRecord" commandargument="<%#Eval("Id") %>">Delete</asp:linkbutton>
</itemtemplate>
</asp:templatefield>
And Note that...
Here don't use commandname="Delete/delete/DELETE".
This are inbuilt keywords. IF you use this keywords it will call OnDelete event not OnRowCommand.
This will occur runtime exception that OnDelete event was not handled.
Your Code To Delete the record.
protected void GVCategories_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument.ToString());
if (e.CommandName == "DeleteRecord")
{
int result = 0;
SqlCommand cmd = new SqlCommand();
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = "DeleteCategory";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add(new SqlParameter("@ID", id));
cmd.CommandTimeout = 10;
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Error occured in DAL.DeleteSelectedRow() \nMessage : " + ex.Message + "InnerException : " + ex.InnerException);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
}
Here your Half work is done.
This will delete data from database.
And to refresh the Grid what you need is get fresh data from database and bind the grid with that fresh data.
Hope This Help You
--------------------
Pratik Bhuva