【问题标题】:How do I update an entity object in GridView如何更新 GridView 中的实体对象
【发布时间】:2013-01-26 03:27:07
【问题描述】:

我正在使用 Entity Framework 和 ASPX 网络表单。在我的GridView(GV) 中,我使用ItemTemplatesEditTemplates 制作我的所有专栏。在编辑模式下,我可以选择一个新值,但它不会更新记录。在 GV 中,我有一个 DropDownList,它设置为与该字段的相关表匹配的 EntityDataSource。我需要哪些步骤,需要处理哪些事件?我已经尝试过RowEditingRowUpdating 事件,但到目前为止还没有有用的代码。如果你想让我给你看一些糟糕的代码 - 只要问,我也会很高兴。我只是需要一些关于接线的指导。

【问题讨论】:

    标签: asp.net entity-framework gridview


    【解决方案1】:

    假设我有一个名为 customerEntities 的 ADO.NET 实体数据模型,它有一个表 Customers 和 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();
            }
    

    【讨论】:

    • 谢谢@Deni。所有字段都会自行更新。我唯一遇到问题的字段是相关表的外键字段。我将处理你的工作的一个变体 - 即使我的名字是对的,但在尝试从行中投射 DDL 时却遇到了砖墙。
    • 我什至发现了这个blog 并试图实现它但没有成功。
    • 我也遇到了外键字段的问题。有没有成功解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多