如何在GridView中更新实体对象

时间:2013-01-26 03:27:07

标签: asp.net entity-framework gridview

我在ASPX webform中使用Entity Framework。在我的GridView(GV)中,我使用ItemTemplatesEditTemplates创建了所有列。在编辑模式下,我可以选择一个新值,但不会更新记录。在GV中,我有DropDownList,其设置为EntityDataSource,与该字段的相关表格相匹配。我需要采取哪些步骤,需要处理哪些事件?我尝试了RowEditingRowUpdating事件,但到目前为止还没有有用的代码。如果你想让我给你看一些不好的代码 - 只要问一下,我也会非常开心。我只需要一些指导即可。

1 个答案:

答案 0 :(得分:0)

假设我有一个名为 - customerEntities的ADO.NET实体数据模型,它有一个表有3列的客户:

  1. 客户ID
  2. 名称
  3. <强> ASPX:

    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateEditButton="true" 
        AutoGenerateColumns="false" onrowcancelingedit="gvCustomers_RowCancelingEdit" 
        onrowediting="gvCustomers_RowEditing" onrowupdating="gvCustomers_RowUpdating" DataKeyNames="CustomerId">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblId" runat="server" Text='<%# Bind("CustomerId") %>' />
                    <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' />
                    <asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
                    <asp:TextBox ID="txtSurname" runat="server" Text='<%# Bind("Surname") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    代码背后:

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    BindCustomers();
            }
    
            private void BindCustomers()
            {
                customerEntities entityModel = new customerEntities();
                gvCustomers.DataSource = entityModel.Customers;
                gvCustomers.DataBind();
            }
    
            protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
            {
                gvCustomers.EditIndex = e.NewEditIndex;
                BindCustomers();
            }
    
            protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                gvCustomers.EditIndex = -1;
                BindCustomers();
            }
    
            protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                int customerId = (int)gvCustomers.DataKeys[e.RowIndex].Value;
                TextBox txtName = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtName");
                TextBox txtSurname = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtSurname");
    
                customerEntities entityModel = new customerEntities();
                Customer customer = entityModel.Customers.Where(c => c.CustomerId == customerId).First();
                customer.Name = txtName.Text;
                customer.Surname = txtSurname.Text;
                entityModel.SaveChanges();
    
                gvCustomers.EditIndex = -1;
                BindCustomers();
            }