2016-03-13 62 views
0

我正在使用Visual Studio 2015和實體框架6.我有一個Gridview我試圖能夠刪除和編輯。我嘗試了各種方法,沒有任何工作。ASP.net EF Gridview行刪除

以下是我有:

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(); 

     } 
    } 

我的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> 

我如何刪除和編輯的行上ASP.net實體框架6的工作?

+0

當你說「我已經嘗試了各種方法,並沒有什麼工作」 ......究竟是什麼讓你嘗試和究竟是什麼「沒有任何工作」的意思(錯誤,一些無法解釋的行爲,缺乏理想的行爲)? –

回答

0

用你的代碼創建新的Pizza對象。

將RowEditing事件添加到您的網格中,並在事件中通過id(優選,將對象ID存儲在網格列中)查找對象,更新其數據並再次保存。

爲編輯:

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(); 

} 

刪除:

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(); 

} 
+0

兩者都沒有工作...並沒有a。刪除,我試圖將其更改爲。刪除,但其餘的都是錯誤的。 – wiredlime2015

+0

您是使用Code First(您創建生成數據庫的類)還是Database First(您有一個數據庫和逆向工程的類)? 另外,你是否檢查事件是否正在觸發,編輯代碼是否正在執行? –

+0

我相信數據庫第一。我有刪除功能現在工作(不同的方式),但更新仍然不是。此時的更新只是將原始值而不是新的類型放入其中。 – wiredlime2015