Insert, Delete, Update in GridView using ASP.NET

Submitted by: 
Visitors have accessed this post 10794 times.

1. As we don't want to use datasource controls. Please delete "ObjectDataSource1" control from the webform.

2. Delete DataSourceID="ObjectDataSource1" from GridView1. This should remove the dependency of GridVIew1 on ObjectDataSource1 control.

3. From the code behind file, delete lbInsert_Click() event handler method.

4. In the "FooterTemplate" of "EmployeeId" TemplateField, please delete OnClick="lbInsert_Click", as we no longer have this event handler method.

5. Delete "CommandField" column from GridView1

6. Now, include a template field in the place of CommandField. This template field is used to display Edit, Update, Cancel and Delete link buttons. We don't want delete and cancel buttons to cause validation, so set CausesValidaion property of these buttons to false.

  1. <asp:TemplateField>
  2. <ItemTemplate>
  3. <asp:LinkButton ID="lbEdit" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="EditRow" ForeColor="#8C4510" runat="server">Edit</asp:LinkButton>
  4. <asp:LinkButton ID="lbDelete" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false">Delete</asp:LinkButton>
  5. </ItemTemplate>
  6. <EditItemTemplate>
  7. <asp:LinkButton ID="lbUpdate" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="UpdateRow" ForeColor="#8C4510" runat="server">Update</asp:LinkButton>
  8. <asp:LinkButton ID="lbCancel" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="CancelUpdate" ForeColor="#8C4510" runat="server" CausesValidation="false">Cancel</asp:LinkButton>
  9. </EditItemTemplate>
  10. </asp:TemplateField>

7. Copy and paste the following private method. This method binds employee data with gridview1 control.

  1. private void BindGridViewData()
  2. {
  3. GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
  4. GridView1.DataBind();
  5. }

8. Call BindGridViewData() in Page_Load() event.

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. BindGridViewData();
  6. }
  7. }

9. Finally generate GridView1_RowCommand() event handler method. Copy and Paste the following code.

  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  2. {
  3. if (e.CommandName == "EditRow")
  4. {
  5. int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
  6. GridView1.EditIndex = rowIndex;
  7. BindGridViewData();
  8. }
  9. else if (e.CommandName == "DeleteRow")
  10. {
  11. EmployeeDataAccessLayer.DeleteEmployee(Convert.ToInt32(e.CommandArgument));
  12. BindGridViewData();
  13. }
  14. else if (e.CommandName == "CancelUpdate")
  15. {
  16. GridView1.EditIndex = -1;
  17. BindGridViewData();
  18. }
  19. else if (e.CommandName == "UpdateRow")
  20. {
  21. int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
  23. int employeeId = Convert.ToInt32(e.CommandArgument);
  24. string name = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox1")).Text;
  25. string gender = ((DropDownList)GridView1.Rows[rowIndex].FindControl("DropDownList1")).SelectedValue;
  26. string city = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox3")).Text;
  28. EmployeeDataAccessLayer.UpdateEmployee(employeeId, name, gender, city);
  30. GridView1.EditIndex = -1;
  31. BindGridViewData();
  32. }
  33. else if (e.CommandName == "InsertRow")
  34. {
  35. string name = ((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;
  36. string gender = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
  37. string city = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
  39. EmployeeDataAccessLayer.InsertEmployee(name, gender, city);
  41. BindGridViewData();
  42. }
  43. }

10. In the FooterTemplate of EmployeeId TemplateField, set CommandName property lbInsert link button to "InsertRow"

  1. <asp:LinkButton ID="lbInsert" CommandName="InsertRow" ForeColor="#8C4510" ValidationGroup="Insert" runat="server">Insert</asp:LinkButton>

11. If you want to show a confirmation dialog box, before a row is deleted, include javascript confirm() function, using "OnClientClick" attribute of LinkButton "lbDelete".

  1. <asp:LinkButton ID="lbDelete" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this row');">Delete</asp:LinkButton>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.