我正在使用Visual Studio 2015和Entity Framework 6.我有一个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();
}
}
我的网格视图:
<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 Entity Framework 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();
}