GridView: Update and Delete data in GridView Using ASP.NET C#

HTML Markup:

  1. <asp:GridView ID="dgvDesignation" class="table table-striped table-bordered table-hover" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnPageIndexChanging="dgvDesignation_PageIndexChanging" OnRowCancelingEdit="dgvDesignation_RowCancelingEdit" OnRowDeleting="dgvDesignation_RowDeleting" OnRowEditing="dgvDesignation_RowEditing" OnRowUpdating="dgvDesignation_RowUpdating">  
  2.     <Columns>  
  3.         <asp:TemplateField HeaderText="Designation_id" Visible="False">  
  4.             <EditItemTemplate>  
  5.                 <asp:Label ID="lblEditDesignationid" runat="server" Text='<%# Bind("Bank_id") %>'></asp:Label>  
  6.             </EditItemTemplate>  
  7.             <ItemTemplate>  
  8.                 <asp:Label ID="lblDesignationid" runat="server" Text='<%# Bind("Bank_id") %>'></asp:Label>  
  9.             </ItemTemplate>  
  10.         </asp:TemplateField>  
  11.         <asp:TemplateField HeaderText="Bank Name">  
  12.             <EditItemTemplate>  
  13.                 <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Bank_name") %>'></asp:TextBox>  
  14.             </EditItemTemplate>  
  15.             <ItemTemplate>  
  16.                 <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Bank_name") %>'></asp:Label>  
  17.             </ItemTemplate>  
  18.         </asp:TemplateField>  
  19.         <asp:TemplateField>  
  20.             <EditItemTemplate>  
  21.                 <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Image/update.jpg" ToolTip="Update" Width="20px" />  
  22.                 <asp:ImageButton ID="imgbtnCancle" runat="server" CommandName="Cancel" ImageUrl="~/Image/Cancel.jpg" ToolTip="Cancel" Width="20px" /> </EditItemTemplate>  
  23.             <ItemTemplate>  
  24.                 <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Image/Edit.jpg" ToolTip="Edit" Width="20px" />  
  25.                 <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Image/delete.jpg" ToolTip="Delete" Width="20px" /> </ItemTemplate>  
  26.             <HeaderStyle Width="60px" />  
  27.             <ItemStyle Width="60px" /> </asp:TemplateField>  
  28.     </Columns>  
  29. </asp:GridView>  
C#
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if(!IsPostBack)  
  4.     {  
  5.         FillGrid();  
  6.         //lblMsg.Visible = false;  
  7.     }  
  8. }  
  9. private void FillGrid()  
  10. {  
  11.     string str = "Select * from tbl_bank order by Bank_name";  
  12.     DataTable dt_Designation = c1.filldt(str);  
  13.     dgvDesignation.DataSource = dt_Designation;  
  14.     dgvDesignation.DataBind();  
  15. }  
  16. private void Save()  
  17. {  
  18.     string str = "Insert into tbl_bank (Bank_name) values('" + txtDesignationName.Text.Trim() + "')";  
  19.     c1.IUD(str);  
  20.     FillGrid();  
  21. }  
  22. protected void dgvDesignation_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  23. {  
  24.     dgvDesignation.PageIndex = e.NewPageIndex;  
  25.     FillGrid();  
  26. }  
  27. protected void dgvDesignation_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  28. {  
  29.     dgvDesignation.EditIndex = -1;  
  30.     FillGrid();  
  31. }  
  32. protected void dgvDesignation_RowEditing(object sender, GridViewEditEventArgs e)  
  33. {  
  34.     dgvDesignation.EditIndex = e.NewEditIndex;  
  35.     FillGrid();  
  36. }  
  37. protected void dgvDesignation_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  38. {  
  39.     Label id = (Label) dgvDesignation.Rows[e.RowIndex].FindControl("lblEditDesignationId");  
  40.     TextBox name = (TextBox) dgvDesignation.Rows[e.RowIndex].FindControl("txtEditDesignationName");  
  41.     string str = "Update tbl_bank set Bank_name='" + name.Text + "' where Bank_id=" + Convert.ToInt32(id.Text);  
  42.     c1.IUD(str);  
  43.     //lblSave.Text = "Record Update Successfully";  
  44.     //lblSave.Visible = true;  
  45.     dgvDesignation.EditIndex = -1;  
  46.     FillGrid();  
  47. }  
  48. protected void dgvDesignation_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  49. {  
  50.     Label id = (Label) dgvDesignation.Rows[e.RowIndex].FindControl("lblDesignationid");  
  51.     string str = "Delete from tbl_bank where Bank_id=" + Convert.ToInt32(id.Text);  
  52.     c1.IUD(str);  
  53.     //lblSave.Text = "Record Delete Successfully";  
  54.     //lblSave.Visible = true;  
  55.     FillGrid();  
  56. }  
  57. protected void btnCancle_Click(object sender, EventArgs e)  
  58. {}  
  59. protected void btnSave_Click(object sender, EventArgs e)  
  60. {  
  61.     Save();  
  62. }  
  63. }