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..
- Create a new website in Visual Studio. I have named it “LinqEditUpdateDelete”.
- Now create a Table “Emp” having following columns.
- Create a new page and paste the following design.
- <asp:Panel ID="PanelEmp" runat="server">
- <br />
- <br />
- <table>
- <tr>
- <td>Employee Name</td>
- <td>
- <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="rfv1" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtEmpName"></asp:RequiredFieldValidator>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td>Salary</td>
- <td>
- <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="rfv2" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtSalary"></asp:RequiredFieldValidator>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td>Designation</td>
- <td>
- <asp:TextBox ID="txtDesg" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vg1" ControlToValidate="txtDesg"></asp:RequiredFieldValidator>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td colspan="3">
- <asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="vg1" OnClick="btnSave_Click" />
- <asp:Button ID="btnCancel" runat="server" Text="Cancel" ValidationGroup="vg2" OnClick="btnCancel_Click" />
- </td>
- </tr>
- </table>
- <br />
- <br />
- <asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false" OnRowEditing="grdEmployee_RowEditing" OnRowCancelingEdit="grdEmployee_RowCancelingEdit" OnRowUpdating="grdEmployee_RowUpdating" OnRowDeleting="grdEmployee_RowDeleting">
- <Columns>
- <asp:TemplateField HeaderText="Employee Name">
- <ItemTemplate>
- <asp:HiddenField ID="hfEmpID" Value='<% #Eval("EmpID") %>' runat="server" />
- <asp:Label ID="lblName" runat="server" Text='<% #Bind("EmpName") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:HiddenField ID="hfEmpID" Value='<% #Eval("EmpID") %>' runat="server" />
- <asp:TextBox ID="txtGridEmpName" runat="server" Text='<% #Bind("EmpName") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
-
- <asp:TemplateField HeaderText="Salary">
- <ItemTemplate>
- <asp:Label ID="lblSalary" runat="server" Text='<% #Bind("Salary") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtGridSalary" runat="server" Text='<% #Bind("Salary") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
-
- <asp:TemplateField HeaderText="Designation">
- <ItemTemplate>
- <asp:Label ID="lblDesg" runat="server" Text='<% #Bind("Designation") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtGridDesg" runat="server" Text='<% #Bind("Designation") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:CommandField ButtonType="Link" ShowEditButton="true" />
- <asp:CommandField ButtonType="Link" ShowDeleteButton="true" />
- </Columns>
- </asp:GridView>
- </asp:Panel>
- Now Add a LinqToSql Classes by right clicking on project then Add New Item > Linq To Sql Classes, name it EmployeeDetails.dbml.
- After creating this now add database connection.
- In the Server Explorer click “Connect to Database”. After filling the server details(usually your PC name) click OK to add connection with database.
- 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.
- Then create an object of Linq to Sql classes. i.e.
EmployeeDetailsDataContext empContext = new EmployeeDetailsDataContext();
- In the Page_Load event of .cs file, create a method for bind grid.
- private void BindGrid()
- {
- var query = from emp in empContext.emps
- select emp;
- if (query.Count() > 0)
- {
- grdEmployee.DataSource = query;
- grdEmployee.DataBind();
- }
- }
- Similarly create the OnRowEditing, OnRowDeleting, OnRowUpdating, OnRowCancelEdit events for grid view. Now use the following logic.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class _Default: Page
- {
- EmployeeDetailsDataContext empContext = new EmployeeDetailsDataContext();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindGrid();
- }
- }
-
- protected void btnSave_Click(object sender, EventArgs e)
- {
- emp employee = new emp();
- employee.empName = txtEmpName.Text;
- employee.designation = txtDesg.Text;
- employee.salary = Convert.ToInt32(txtSalary.Text);
- empContext.emps.InsertOnSubmit(employee);
- empContext.SubmitChanges();
- BindGrid();
- txtDesg.Text = string.Empty;
- txtEmpName.Text = string.Empty;
- txtSalary.Text = string.Empty;
- }
-
- protected void btnCancel_Click(object sender, EventArgs e)
- {
- txtDesg.Text = string.Empty;
- txtEmpName.Text = string.Empty;
- txtSalary.Text = string.Empty;
- }
-
- private void BindGrid()
- {
- var query = from emp in empContext.emps
- select emp;
-
- if (query.Count() > 0)
- {
- grdEmployee.DataSource = query;
- grdEmployee.DataBind();
- }
- }
-
- protected void grdEmployee_RowEditing(object sender, GridViewEditEventArgs e)
- {
- grdEmployee.EditIndex = e.NewEditIndex;
- BindGrid();
- }
-
- protected void grdEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- grdEmployee.EditIndex = -1;
- BindGrid();
- }
-
- protected void grdEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- HiddenField hfEID = (HiddenField) grdEmployee.Rows[e.RowIndex].FindControl("hfEmpID");
- TextBox txtGrdName = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridEmpName");
- TextBox txtGrdDesg = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridDesg");
- TextBox txtGrdSalary = (TextBox) grdEmployee.Rows[e.RowIndex].FindControl("txtGridSalary");
- emp EmpVar = (from em in empContext.emps where em.empid == Convert.ToInt32(hfEID.Value) select em).FirstOrDefault();
-
- EmpVar.empName = txtGrdName.Text;
- EmpVar.designation = txtGrdDesg.Text;
- EmpVar.salary = Convert.ToInt32(txtGrdSalary.Text);
- empContext.SubmitChanges();
-
- grdEmployee.EditIndex = -1;
- BindGrid();
- }
-
- protected void grdEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- HiddenField hfEID = (HiddenField) grdEmployee.Rows[e.RowIndex].FindControl("hfEmpID");
- emp EmpVar = (from em in empContext.emps where em.empid == Convert.ToInt32(hfEID.Value) select em).FirstOrDefault();
-
- empContext.emps.DeleteOnSubmit(EmpVar);
- empContext.SubmitChanges();
- BindGrid();
- }
- }
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.