Hi here is the Simple Solution to Implement:
► Add a linkButton inside the Item Template in Gridview
<asp:LinkButton ID="lnkDelete" CommandName="Delete" Text="Delete" runat="server"/>
► Add javascript for linkButton in RowDataBound Event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record " +
DataBinder.Eval(e.Row.DataItem, "id") + "')");
}
}
► Implement the RowCommand Event
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int rowIndex = int.Parse(row.RowIndex.ToString());
string id = ((Label)row.FindControl("lblid")).Text;
SqlConnection connect = new SqlConnection(ConnString);
connect.Open();
string q = "Delete people where id=@id";
SqlCommand comm = new SqlCommand(q, connect);
comm.Parameters.AddWithValue("id", id);
comm.ExecuteNonQuery();
connect.Close();
GridView1.EditIndex = -1;
//BindGrid();
}
}