Edit,Update&Delete in Gridview using EDM with LINQ


Code in ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="EmpNo" ReadOnly="true"
HeaderText="EmpId"/>
<asp:BoundField DataField="EmpName" HeaderText="Empname" />
<asp:BoundField DataField="EmpJob" HeaderText="EmpJob" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" />
<asp:CommandField ShowEditButton="true"
ShowDeleteButton="true" ShowCancelButton="true" />
</Columns>
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
 ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

    </div>
    </form>
</body>
</html>

Code Behind:

DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
    Class1 obj3 = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    public void GetData()
    {
        var x = (from n in obj.Emps
                 select n);
        GridView1.DataSource = x;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string Id = (GridView1.Rows[e.RowIndex].Cells[0]).Text;
        TextBox tbname = (TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0]);
        TextBox tbjob = (TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]);
        TextBox tbsal = (TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0]);
        DatabaseModel.Emp obj1 = new DatabaseModel.Emp();
        obj1.EmpNo = int.Parse(Id);
        obj1.EmpName = tbname.Text;
        obj1.EmpJob = tbjob.Text;
        obj1.EmpSal = Convert.ToDecimal(tbsal.Text);
        obj3.Update(obj1);
        GridView1.EditIndex = -1;
        GetData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GetData();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GetData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string Id = (GridView1.Rows[e.RowIndex].Cells[0]).Text;
        DatabaseModel.Emp obj2 = new DatabaseModel.Emp();
        obj2.EmpNo = int.Parse(Id);
        obj3.Delete(obj2);
        GetData();
        GridView1.EditIndex = -1;
    }
Code in Class1.cs:
public void Update(DatabaseModel.Emp empdetails)
    {
        var x = (from n in obj.Emps
                 where n.EmpNo == empdetails.EmpNo
                 select n).FirstOrDefault();
        if (x != null)
        {
            x.EmpName = empdetails.EmpName;
            x.EmpJob = empdetails.EmpJob;
            x.EmpSal = empdetails.EmpSal;
            obj.SaveChanges();
        }
    }
    public void Delete(DatabaseModel.Emp empid)
    {
        var x = (from n in obj.Emps
                 where n.EmpNo == empid.EmpNo
                 select n).FirstOrDefault();
        if (x != null)
        {
            obj.DeleteObject(x);
            obj.SaveChanges();
        }
    }

DownloadSampleCode


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s