Here in this application I will use an Item template, Edit Item template and Footer Template to do CRUD operations.

The following is my Data Table in design mode:

emp id
Image 1.

Data in my Data Table:

My Data Table
Image 2.

To create the new application go to Solution Explorer in your application then select Add New Item.

Add an ADO.NET Entity Data Model.

Entity Data Model
Image 3.

generate database
Image 4.

new connection
Image 5.

Enter your server information and select your database:

select your Data base
Image 6.

select data connection
Image 7.

table
Image 8.

property
Image 9.

Now open Default.aspx and do the following code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRUDUsingEntityFramework.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>CRUD Operation Using Entity Framework In ASP.NET Grid View</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
  11. <tr>
  12. <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px;
  13. background-color: skyblue; font-family: 'Times New Roman';
  14. font-size: 20pt; color: red;">CRUD Operation Using Entity Framework In ASP.NET Grid View
  15. </td>
  16. </tr>
  17. <tr>
  18. <td style="text-align: left;">
  19. <asp:GridView ID="GVEmployee" runat="server"
  20. AutoGenerateColumns="False" ShowFooter="True"
  21. PageSize="10" AllowPaging="true"
  22. OnRowCommand="GVEmployee_RowCommand"
  23. DataKeyNames="Emp_ID" CellPadding="4" ForeColor="#333333"
  24. GridLines="None" OnRowCancelingEdit="GVEmployee_RowCancelingEdit"
  25. OnRowEditing="GVEmployee_RowEditing"
  26. OnRowUpdating="GVEmployee_RowUpdating"
  27. OnRowDeleting="GVEmployee_RowDeleting"
  28. OnPageIndexChanging="GVEmployee_OnPageIndexChanging">
  29. <AlternatingRowStyle BackColor="White" />
  30. <Columns>
  31. <asp:TemplateField HeaderText="Employee Name" HeaderStyle-HorizontalAlign="Left">
  32. <EditItemTemplate>
  33. <asp:TextBox ID="txtEmpName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
  34. <asp:RequiredFieldValidator ID="valEmpName" runat="server" ControlToValidate="txtEmpName"
  35. Display="Dynamic" ErrorMessage="Employee Name is required." ForeColor="Red" SetFocusOnError="True"
  36. ValidationGroup="vldEditRecord">*</asp:RequiredFieldValidator>
  37. </EditItemTemplate>
  38. <ItemTemplate>
  39. <asp:Label ID="lblEmpName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
  40. </ItemTemplate>
  41. <FooterTemplate>
  42. <asp:TextBox ID="txtEmpNameNew" runat="server"></asp:TextBox>
  43. <asp:RequiredFieldValidator ID="valEmpNameNew" runat="server" ControlToValidate="txtEmpNameNew"
  44. Display="Dynamic" ErrorMessage="Employee Name is required." ForeColor="Red" SetFocusOnError="True"
  45. ValidationGroup="vldNewRecord">*</asp:RequiredFieldValidator>
  46. </FooterTemplate>
  47. </asp:TemplateField>
  48. <asp:TemplateField HeaderText="Designation" HeaderStyle-HorizontalAlign="Left">
  49. <EditItemTemplate>
  50. <asp:TextBox ID="txtDesignation" runat="server" Text='<%# Bind("Designation") %>'></asp:TextBox>
  51. <asp:RequiredFieldValidator ID="valDesignation" runat="server" ControlToValidate="txtDesignation"
  52. Display="Dynamic" ErrorMessage="Designation is required." ForeColor="Red" SetFocusOnError="True"
  53. ValidationGroup="vldEditRecord">*</asp:RequiredFieldValidator>
  54. </EditItemTemplate>
  55. <ItemTemplate>
  56. <asp:Label ID="lblDesignation" runat="server" Text='<%# Bind("Designation") %>'></asp:Label>
  57. </ItemTemplate>
  58. <FooterTemplate>
  59. <asp:TextBox ID="txtDesignationNew" runat="server"></asp:TextBox>
  60. <asp:RequiredFieldValidator ID="valDesignationNew" runat="server" ControlToValidate="txtDesignationNew"
  61. Display="Dynamic" ErrorMessage="Designation is required." ForeColor="Red" SetFocusOnError="True"
  62. ValidationGroup="vldNewRecord">*</asp:RequiredFieldValidator>
  63. </FooterTemplate>
  64. </asp:TemplateField>
  65. <asp:TemplateField HeaderText="City" HeaderStyle-HorizontalAlign="Left">
  66. <EditItemTemplate>
  67. <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
  68. <asp:RequiredFieldValidator ID="valCity" runat="server" ControlToValidate="txtCity"
  69. Display="Dynamic" ErrorMessage="City is required." ForeColor="Red" SetFocusOnError="True"
  70. ValidationGroup="vldEditRecord">*</asp:RequiredFieldValidator>
  71. </EditItemTemplate>
  72. <ItemTemplate>
  73. <asp:Label ID="lblCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
  74. </ItemTemplate>
  75. <FooterTemplate>
  76. <asp:TextBox ID="txtCityNew" runat="server"></asp:TextBox>
  77. <asp:RequiredFieldValidator ID="valCityNew" runat="server" ControlToValidate="txtCityNew"
  78. Display="Dynamic" ErrorMessage="City is required." ForeColor="Red" SetFocusOnError="True"
  79. ValidationGroup="vldNewRecord">*</asp:RequiredFieldValidator>
  80. </FooterTemplate>
  81. </asp:TemplateField>
  82. <asp:TemplateField HeaderText="State" HeaderStyle-HorizontalAlign="Left">
  83. <EditItemTemplate>
  84. <asp:TextBox ID="txtState" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>
  85. <asp:RequiredFieldValidator ID="valState" runat="server" ControlToValidate="txtState"
  86. Display="Dynamic" ErrorMessage="State is required." ForeColor="Red" SetFocusOnError="True"
  87. ValidationGroup="vldEditRecord">*</asp:RequiredFieldValidator>
  88. </EditItemTemplate>
  89. <ItemTemplate>
  90. <asp:Label ID="lblState" runat="server" Text='<%# Bind("State") %>'></asp:Label>
  91. </ItemTemplate>
  92. <FooterTemplate>
  93. <asp:TextBox ID="txtStateNew" runat="server"></asp:TextBox>
  94. <asp:RequiredFieldValidator ID="valStateNew" runat="server" ControlToValidate="txtStateNew"
  95. Display="Dynamic" ErrorMessage="State is required." ForeColor="Red" SetFocusOnError="True"
  96. ValidationGroup="vldNewRecord">*</asp:RequiredFieldValidator>
  97. </FooterTemplate>
  98. </asp:TemplateField>
  99. <asp:TemplateField HeaderText="Country" HeaderStyle-HorizontalAlign="Left">
  100. <EditItemTemplate>
  101. <asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
  102. <asp:RequiredFieldValidator ID="valCountry" runat="server" ControlToValidate="txtCountry"
  103. Display="Dynamic" ErrorMessage="Country is required." ForeColor="Red" SetFocusOnError="True"
  104. ValidationGroup="vldEditRecord">*</asp:RequiredFieldValidator>
  105. </EditItemTemplate>
  106. <ItemTemplate>
  107. <asp:Label ID="lblCountry" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
  108. </ItemTemplate>
  109. <FooterTemplate>
  110. <asp:TextBox ID="txtCountryNew" runat="server"></asp:TextBox>
  111. <asp:RequiredFieldValidator ID="valCountryNew" runat="server" ControlToValidate="txtCountryNew"
  112. Display="Dynamic" ErrorMessage="Country is required." ForeColor="Red" SetFocusOnError="True"
  113. ValidationGroup="vldNewRecord">*</asp:RequiredFieldValidator>
  114. </FooterTemplate>
  115. </asp:TemplateField>
  116. <asp:TemplateField HeaderText="">
  117. <ItemTemplate>
  118. <asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Edit">
  119. <img src="../Images/Edit.png" width="30px" />
  120. </asp:LinkButton>
  121. <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
  122. ToolTip="Delete" OnClientClick='return confirm("Are you sure you want to delete employee record?");'>
  123. <img src="../Images/Delete.jpg" width="30px" />
  124. </asp:LinkButton>
  125. </ItemTemplate>
  126. <EditItemTemplate>
  127. <asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="vldEditRecord"
  128. CommandName="Update" ToolTip="Save"
  129. OnClientClick='return confirm("Employee Record Saved Successfully.");'>
  130. <img src="../Images/Save2.jpg" width="30px" />
  131. </asp:LinkButton>
  132. <asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="Cancel" ToolTip="Cancel">
  133. <img src="../Images/Cancel.jpg" width="30px" />
  134. </asp:LinkButton>
  135. </EditItemTemplate>
  136. <FooterTemplate>
  137. <asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="vldNewRecord"
  138. CommandName="InsertNew" ToolTip="Add New Employee"
  139. OnClientClick='return confirm("Employee Record addedd Successfully.");'>
  140. <img src="../Images/Insert.jpg" width="30px" />
  141. </asp:LinkButton>
  142. <asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="CancelNew" ToolTip="Cancel">
  143. <img src="../Images/Cancel.jpg" width="30px" />
  144. </asp:LinkButton>
  145. </FooterTemplate>
  146. </asp:TemplateField>
  147. </Columns>
  148. <EditRowStyle BackColor="#2461BF" />
  149. <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  150. <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  151. <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  152. <RowStyle BackColor="#EFF3FB" />
  153. <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  154. <SortedAscendingCellStyle BackColor="#F5F7FB" />
  155. <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  156. <SortedDescendingCellStyle BackColor="#E9EBEF" />
  157. <SortedDescendingHeaderStyle BackColor="#4870BE" />
  158. </asp:GridView>
  159. </td>
  160. </tr>
  161. </table>
  162. </div>
  163. </form>
  164. </body>
  165. </html>
Now my aspx.cs code is:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace CRUDUsingEntityFramework
  8. {
  9. public partial class Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack)
  14. {
  15. BindGrid();
  16. }
  17. }
  18. void BindGrid()
  19. {
  20. using (EmployeeManagementEntities context = new EmployeeManagementEntities())
  21. {
  22. if (context.Employee.Count() > 0)
  23. {
  24. GVEmployee.DataSource = (from em in context.Employee
  25. select new { em.Emp_ID, em.Name, em.Designation, em.City, em.Country, em.State }).ToList();
  26. GVEmployee.DataBind();
  27. }
  28. else
  29. {
  30. GVEmployee.DataSource = null;
  31. GVEmployee.DataBind();
  32. }
  33. }
  34. }
  35. protected void GVEmployee_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
  36. {
  37. GVEmployee.PageIndex = e.NewPageIndex;
  38. BindGrid();
  39. }
  40. protected void GVEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
  41. {
  42. if (e.CommandName == "InsertNew")
  43. {
  44. GridViewRow row = GVEmployee.FooterRow;
  45. TextBox txtName = row.FindControl("txtEmpNameNew") as TextBox;
  46. TextBox txtDesignation = row.FindControl("txtDesignationNew") as TextBox;
  47. TextBox txtCity = row.FindControl("txtCityNew") as TextBox;
  48. TextBox txtState = row.FindControl("txtStateNew") as TextBox;
  49. TextBox txtCountry = row.FindControl("txtCountryNew") as TextBox;
  50. using (EmployeeManagementEntities context = new EmployeeManagementEntities())
  51. {
  52. Employee obj = new Employee();
  53. obj.Name = txtName.Text;
  54. obj.Designation = txtDesignation.Text;
  55. obj.City = txtCity.Text;
  56. obj.State = txtState.Text;
  57. obj.Country = txtCountry.Text;
  58. context.Employee.Add(obj);
  59. context.SaveChanges();
  60. BindGrid();
  61. }
  62. }
  63. }
  64. protected void GVEmployee_RowEditing(object sender, GridViewEditEventArgs e)
  65. {
  66. GVEmployee.EditIndex = e.NewEditIndex;
  67. BindGrid();
  68. }
  69. protected void GVEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  70. {
  71. GVEmployee.EditIndex = -1;
  72. BindGrid();
  73. }
  74. protected void GVEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
  75. {
  76. GridViewRow row = GVEmployee.Rows[e.RowIndex];
  77. TextBox txtName = row.FindControl("txtEmpName") as TextBox;
  78. TextBox txtDesignation = row.FindControl("txtDesignation") as TextBox;
  79. TextBox txtCity = row.FindControl("txtCity") as TextBox;
  80. TextBox txtState = row.FindControl("txtState") as TextBox;
  81. TextBox txtCountry = row.FindControl("txtCountry") as TextBox;
  82. using (EmployeeManagementEntities context = new EmployeeManagementEntities())
  83. {
  84. int employeeID = Convert.ToInt32(GVEmployee.DataKeys[e.RowIndex].Value);
  85. Employee obj = context.Employee.First(x => x.Emp_ID == employeeID);
  86. obj.Name = txtName.Text;
  87. obj.Designation = txtDesignation.Text;
  88. obj.City = txtCity.Text;
  89. obj.State = txtState.Text;
  90. obj.Country = txtCountry.Text;
  91. context.SaveChanges();
  92. GVEmployee.EditIndex = -1;
  93. BindGrid();
  94. }
  95. }
  96. protected void GVEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
  97. {
  98. int employeeID = Convert.ToInt32(GVEmployee.DataKeys[e.RowIndex].Value);
  99. using (EmployeeManagementEntities context = new EmployeeManagementEntities())
  100. {
  101. Employee obj = context.Employee.First(x => x.Emp_ID == employeeID);
  102. context.Employee.Remove(obj);
  103. context.SaveChanges();
  104. BindGrid();
  105. }
  106. }
  107. }
  108. }
Now run your application: All Record

crud operation
Image 10.

Add a new record:

record
Image 11.

Go to Page 2 as I have Page Size 10 here.

entity framwork
Image 12.

Now edit any record:

edit any record
Image 13.

page at localhost
Image 14.

Now delete any record:

delete any record
Image 15.