0

I am using Visual Studio 2015 and Entity Framework 6. I have a Gridview I am trying to be able to delete from and edit. I have tried various methods and nothing is working.

Here is what I have:

  protected void gvExOr_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        using (PizzaParlor2Entities po = new PizzaParlor2Entities())
        {
            var PizzaID = Convert.ToInt32(gvExOr.Rows[e.RowIndex].Cells[0].Text.ToString());
            //  var SizeID = Convert.ToInt32(gvExOr.Rows[e.RowIndex].Cells[1].Text.ToString());


            Pizza newPiz = new Pizza()
            {
                PizzaID = PizzaID
            };


            po.SaveChanges();

        }
    }

My gridview:

  <asp:GridView ID="gvExOr" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
                DataKeyNames="PizzaID,OrderID" OnRowDeleting="gvExOr_RowDeleting"  OnRowUpdating="gvExOr_RowUpdating"
                AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false">

    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="true"  />

        <asp:BoundField DataField="PizzaID" HeaderText="PizzaID" ReadOnly="True" SortExpression="PizzaID" />
        <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" SortExpression="FirstName" />
         <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" SortExpression="LastName" />
        <asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" />
        <asp:BoundField DataField="Crust" HeaderText="Crust" SortExpression="Crust" />
        <asp:BoundField DataField="Sauce" HeaderText="Sauce" SortExpression="Sauce" />
        <asp:CheckBoxField DataField="Delivery" HeaderText="Delivery" SortExpression="Delivery" />
        <asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" DataFormatString="{0:c}" SortExpression="OrderPrice" />
    </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>

How do I make Deletion and Editing of rows work on ASP.net Entity Framework 6?

1
  • When you say "I have tried various methods and nothing is working."... what exactly have you tried and what exactly does "nothing is working" mean (errors, some unexplained behavior, lack of desired behavior)?
    – B.K.
    Mar 13, 2016 at 5:04

1 Answer 1

0

With your code you are creating new Pizza objects.

Add the RowEditing event to your grid and in the event find the object by the id (preferible, store the object id in a grid column), update its data and save it again.

For editing:

using (PizzaParlor2Entities po = new PizzaParlor2Entities())
{
    var PizzaID = Convert.ToInt32(gvExOr.Rows[e.RowIndex].Cells[0].Text.ToString());
    var oldPizza = po.Pizza.Find(PizzaID); // retrieve the object by the id
    // update the needed data
    oldPizza.Price =  gvExOr.Rows[e.RowIndex].Cells[1].Text.ToString();

    //save changes to DB
    po.SaveChanges();

}

For deleting:

using (PizzaParlor2Entities po = new PizzaParlor2Entities())
{
    var PizzaID = Convert.ToInt32(gvExOr.Rows[e.RowIndex].Cells[0].Text.ToString());
    po.Pizza.Delete(p => p.PizzaID == PizzaID); // delete the object by the id
    //save changes to DB
    po.SaveChanges();

}
4
  • Neither is working... and there isnt a .Delete, i tried to change it to the .Remove, but then the rest is wrong too. Mar 13, 2016 at 7:40
  • Are you using Code First (you create the classes that generate the database) or Database First (you have a database en reverse engineer to classes) ? Also, did you check that the events are firing and that the editing code are being executed ? Mar 13, 2016 at 16:59
  • I believe Database first. I have the delete feature working now (different way), but updating still is not. The updating at this point just puts back the original value instead of the new types in one. Mar 13, 2016 at 20:12
  • Can I see your delete code, and from there we can figure out the edit part Mar 14, 2016 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.