ASP.net EF6 Gridview更新行

时间:2016-03-13 21:01:31

标签: c# asp.net visual-studio-2015 entity-framework-6

我正在使用Visual Studio 2015,Entity Framework 6和C#。我有一个将删除的gridview,但我需要它来编辑单击编辑时的行。我制作了一个存储过程,但我也无法在其中工作。

我有两个存储过程(一个用于测试):

 spUpdatePizza3(Size, PizzaID)
 spUpdatePizza2(Size, Crust, Sauce, OrderCost,PizzaID)

这就是我所拥有的:

 protected void gvExOr_RowEditing(object sender, GridViewEditEventArgs e)
    {

        using (PizzaParlor2Entities po = new PizzaParlor2Entities())
        {

            String PizzaID = gvExOr.Rows[e.NewEditIndex].Cells[2].Text.ToString();
            String Size = gvExOr.Rows[e.NewEditIndex].Cells[6].Text.ToString();

            //  String Crust = gvExOr.Rows[e.NewEditIndex].Cells[7].Text.ToString();
            //  String Sauce = gvExOr.Rows[e.NewEditIndex].Cells[8].Text.ToString();
            // String Delivery = gvExOr.Rows[e.NewEditIndex].Cells[9].Text.ToString();
            //   String OrderCost = gvExOr.Rows[e.NewEditIndex].Cells[10].Text.ToString();

          //  string size = gvExOr.SelectedRow.Cells[6].Text;
         //   lblTest.Text = size;

            gvExOr.EditIndex = e.NewEditIndex;

            //  po.spUpdatePizza3(Size, Convert.ToInt32(PizzaID));
            //  po.spUpdatePizza2(Size, Crust, Sauce, Convert.ToDecimal(OrderCost), Convert.ToInt32(PizzaID));

        }

        gvExOr.DataBind();

    }

问题:如何使用Entity Framework 6使用存储过程(或此时的任何方法)更新行?

1 个答案:

答案 0 :(得分:1)

使用OnRowUpdating事件实际保存数据:

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gvExOr.Rows[e.RowIndex];
    var pizzaId = gvExOr.DataKeys[e.RowIndex].Values[0];  // Set this in markup of grid
    string size = (row.FindControl("txtSize") as TextBox).Text;  // Assumes you have EditItemTemplate
    using (PizzaParlor2Entities entities = new PizzaParlor2Entities ())
    {
        Pizza pizza = entities.Pizzas.First(p => p.PizzaID == pizzaID);
        pizza.Size = size;
        entities.SaveChanges();
    }
    gvExOr.EditIndex = -1;
    this.BindGrid();
}

请参阅http://www.aspsnippets.com/Articles/Select-Insert-Edit-Update-and-Delete-in-GridView-using-Entity-Framework-in-ASPNet.aspx