Edit, Update, and Delete in GridView using LINQ

In this tutorial we will learn how to edit, update, and delete operations in GridView using Linq. Linq is a Microsoft technology that adds Query capabilities in the code. Let’s start..

  1. Create a new website in Visual Studio. I have named it “LinqEditUpdateDelete”.

  2. Now create a Table “Emp” having following columns.

    table

  3. Create a new page and paste the following design.
    1. <asp:Panel ID="PanelEmp" runat="server">  
    2.     <br />  
    3.     <br />  
    4.     <table>  
    5.         <tr>  
    6.             <td>Employee Name</td>  
    7.             <td>  
    8.                 <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>  
    9.                 <asp:RequiredFieldValidator ID="rfv1" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtEmpName"></asp:RequiredFieldValidator>  
    10.             </td>  
    11.             <td> </td>  
    12.         </tr>  
    13.         <tr>  
    14.             <td>Salary</td>  
    15.             <td>  
    16.                 <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>  
    17.                 <asp:RequiredFieldValidator ID="rfv2" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtSalary"></asp:RequiredFieldValidator>  
    18.             </td>  
    19.             <td> </td>  
    20.         </tr>  
    21.         <tr>  
    22.             <td>Designation</td>  
    23.             <td>  
    24.                 <asp:TextBox ID="txtDesg" runat="server"></asp:TextBox>  
    25.                 <asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtDesg"></asp:RequiredFieldValidator>  
    26.             </td>  
    27.             <td> </td>  
    28.         </tr>  
    29.         <tr>  
    30.             <td colspan="3">  
    31.                 <asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="vg1" OnClick="btnSave_Click" />  
    32.                 <asp:Button ID="btnCancel" runat="server" Text="Cancel" ValidationGroup="vg2" OnClick="btnCancel_Click" />  
    33.             </td>  
    34.         </tr>  
    35.     </table>  
    36.     <br />  
    37.     <br />  
    38.     <asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false" OnRowEditing="grdEmployee_RowEditing" OnRowCancelingEdit="grdEmployee_RowCancelingEdit" OnRowUpdating="grdEmployee_RowUpdating" OnRowDeleting="grdEmployee_RowDeleting">  
    39.         <Columns>  
    40.             <asp:TemplateField HeaderText="Employee Name">  
    41.                 <ItemTemplate>  
    42.                     <asp:HiddenField ID="hfEmpID" Value='<% #Eval("EmpID") %>' runat="server" />  
    43.                     <asp:Label ID="lblName" runat="server" Text='<% #Bind("EmpName") %>'></asp:Label>  
    44.                 </ItemTemplate>  
    45.                 <EditItemTemplate>  
    46.                     <asp:HiddenField ID="hfEmpID" Value='<% #Eval("EmpID") %>' runat="server" />  
    47.                     <asp:TextBox ID="txtGridEmpName" runat="server" Text='<% #Bind("EmpName") %>'></asp:TextBox>  
    48.                 </EditItemTemplate>  
    49.             </asp:TemplateField>  
    50.   
    51.             <asp:TemplateField HeaderText="Salary">  
    52.                 <ItemTemplate>  
    53.                     <asp:Label ID="lblSalary" runat="server" Text='<% #Bind("Salary") %>'></asp:Label>  
    54.                 </ItemTemplate>  
    55.                 <EditItemTemplate>  
    56.                     <asp:TextBox ID="txtGridSalary" runat="server" Text='<% #Bind("Salary") %>'></asp:TextBox>  
    57.                 </EditItemTemplate>  
    58.             </asp:TemplateField>  
    59.   
    60.             <asp:TemplateField HeaderText="Designation">  
    61.                 <ItemTemplate>  
    62.                     <asp:Label ID="lblDesg" runat="server" Text='<% #Bind("Designation") %>'></asp:Label>  
    63.                 </ItemTemplate>  
    64.                 <EditItemTemplate>  
    65.                     <asp:TextBox ID="txtGridDesg" runat="server" Text='<% #Bind("Designation") %>'></asp:TextBox>  
    66.                 </EditItemTemplate>  
    67.             </asp:TemplateField>  
    68.             <asp:CommandField ButtonType="Link" ShowEditButton="true" />  
    69.             <asp:CommandField ButtonType="Link" ShowDeleteButton="true" />  
    70.         </Columns>  
    71.     </asp:GridView>  
    72. </asp:Panel>  
  4. Now Add a LinqToSql Classes by right  clicking  on project then Add New Item > Linq To Sql Classes, name it EmployeeDetails.dbml.

  5. After creating this now add database connection.

  6. In the Server Explorer click “Connect to Database”. After filling the server details(usually your PC name) click OK to add connection with database.

    connection

  7. After creating connection expand the Tables tab of the connected database in Server and drag that table to the Linq to Sql class where you want do the Edit, delete, Update operations.

    emp

  8. Then create an object of Linq to Sql classes. i.e.
    EmployeeDetailsDataContext empContext = new EmployeeDetailsDataContext();

  9. In the Page_Load event of .cs file, create a method for bind grid.
    1. private void BindGrid()  
    2. {  
    3.     var query = from emp in empContext.emps  
    4.     select emp;  
    5.     if (query.Count() > 0)  
    6.     {  
    7.         grdEmployee.DataSource = query;  
    8.         grdEmployee.DataBind();  
    9.     }  
    10. }  
  10. Similarly create the OnRowEditing, OnRowDeleting, OnRowUpdating, OnRowCancelEdit events for grid view. Now use the following logic.
    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.   
    8. public partial class _Default: Page  
    9. {  
    10.     EmployeeDetailsDataContext empContext = new EmployeeDetailsDataContext();  
    11.     protected void Page_Load(object sender, EventArgs e)  
    12.     {  
    13.         if (!IsPostBack)  
    14.         {  
    15.             BindGrid();  
    16.         }  
    17.     }  
    18.   
    19.     protected void btnSave_Click(object sender, EventArgs e)   
    20.     {  
    21.         emp employee = new emp();  
    22.         employee.empName = txtEmpName.Text;  
    23.         employee.designation = txtDesg.Text;  
    24.         employee.salary = Convert.ToInt32(txtSalary.Text);  
    25.         empContext.emps.InsertOnSubmit(employee);  
    26.         empContext.SubmitChanges();  
    27.         BindGrid();  
    28.         txtDesg.Text = string.Empty;  
    29.         txtEmpName.Text = string.Empty;  
    30.         txtSalary.Text = string.Empty;  
    31.     }  
    32.   
    33.     protected void btnCancel_Click(object sender, EventArgs e)  
    34.     {  
    35.         txtDesg.Text = string.Empty;  
    36.         txtEmpName.Text = string.Empty;  
    37.         txtSalary.Text = string.Empty;  
    38.     }  
    39.   
    40.     private void BindGrid()  
    41.     {  
    42.         var query = from emp in empContext.emps  
    43.         select emp;  
    44.   
    45.         if (query.Count() > 0)  
    46.         {  
    47.             grdEmployee.DataSource = query;  
    48.             grdEmployee.DataBind();  
    49.         }  
    50.     }  
    51.   
    52.     protected void grdEmployee_RowEditing(object sender, GridViewEditEventArgs e)  
    53.     {  
    54.         grdEmployee.EditIndex = e.NewEditIndex;  
    55.         BindGrid();  
    56.     }  
    57.   
    58.     protected void grdEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
    59.     {  
    60.         grdEmployee.EditIndex = -1;  
    61.         BindGrid();  
    62.     }  
    63.   
    64.     protected void grdEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)  
    65.     {  
    66.         HiddenField hfEID = (HiddenField) grdEmployee.Rows[e.RowIndex].FindControl("hfEmpID");  
    67.         TextBox txtGrdName = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridEmpName");  
    68.         TextBox txtGrdDesg = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridDesg");  
    69.         TextBox txtGrdSalary = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridSalary");  
    70.         emp EmpVar = (from em in empContext.emps where em.empid == Convert.ToInt32(hfEID.Value) select em).FirstOrDefault();  
    71.   
    72.         EmpVar.empName = txtGrdName.Text;  
    73.         EmpVar.designation = txtGrdDesg.Text;  
    74.         EmpVar.salary = Convert.ToInt32(txtGrdSalary.Text);  
    75.         empContext.SubmitChanges();  
    76.   
    77.         grdEmployee.EditIndex = -1;  
    78.         BindGrid();  
    79.     }  
    80.   
    81.     protected void grdEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)  
    82.     {  
    83.         HiddenField hfEID = (HiddenField) grdEmployee.Rows[e.RowIndex].FindControl("hfEmpID");  
    84.         emp EmpVar = (from em in empContext.emps where em.empid == Convert.ToInt32(hfEID.Value) select em).FirstOrDefault();  
    85.   
    86.         empContext.emps.DeleteOnSubmit(EmpVar);  
    87.         empContext.SubmitChanges();  
    88.         BindGrid();  
    89.     }  
    90. }  
    Cheers --  we have done all the coding , now run your project and do the operations.

    Thank you very much, and if you have query or any suggestion then you’re most welcome to please comment.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now