如何创建一个嵌套的GridView来编辑EF Code First关系?

| 我有一个经典的父子关系,我想使用
asp:GridView
控件进行CRUD。要CRUD父级很容易,但是挑战在于将to0嵌套在能够处理子关系的relation0中。 为了简化问题,我构建了一个示例。考虑以下EF代码:
public class Context : DbContext
{         
    public DbSet<Animal> Animals { get; set; }
    public DbSet<Tag> Tags { get; set; }
}

public class Animal
{
    public int AnimalID { get; set; }
    public string Name { get; set; }
    public virtual IEnumerable<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagID { get; set; }
    public string Name { get; set; }
}
我正在使用
asp:Gridview
查看/编辑
Animal
对象:
<asp:GridView runat=\"server\" DataSourceID=\"animalDataSource\" DataKeyNames=\"AnimalID\" AutoGenerateColumns=\"false\">   
    <Columns>
        <asp:BoundField DataField=\"Description\" HeaderText=\"Description\" />
        <asp:CommandField ShowCancelButton=\"true\" ShowEditButton=\"true\" ShowDeleteButton=\"true\" />
    </Columns>
</asp:GridView>
ѭ7与后面的代码绑定:
protected void DataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e) 
{    
        var context = new Context();
        e.Context = ((IObjectContextAdapter)context).ObjectContext; } 
}
我想在其中添加一个嵌套的ѭ4to,以添加/删除/编辑属于那个
Animal
Tag
对象。我怎样才能做到这一点?     
已邀请:
        BoundField将指定的DataSource字段的值显示为文本。通过使用绑定字段,我们可以使用标题文本和数据字段直接绑定数据,而无需使用任何控件。 。 TemplateField允许混合使用HTML标记,Web控件和数据绑定语法。我们可以在模板字段中定义我们自己的asp.net控件。因此,基本上,您将绑定字段转换为模板列模板列还带有一个编辑模板标签,该标签可为您提供超出该gridview行所需的标准编辑功能的更多信息...例如,在编辑模式下,在此行中放置一个下拉列表供我选择-无限的可能性 更改为模板字段去编辑 模板将网格控件添加到字段 向其添加“编辑/删除链接”按钮 在编辑模板下继续嵌套网格的属性 查找更新,行databounfd事件等 我认为这会有所帮助   Dim grd1作为GridViewRow
        Dim gv As GridView
        Dim l1, l2 As Label
        Dim strsql As String
        For Each grd1 In GridView1.Rows
            \'find controls of parent gridrow
            l1 = grd1.FindControl(\"l00\")
            l2 = grd1.FindControl(\"l1\")
            gv = grd1.FindControl(\"gv1\")
            strsql = \"select file_name from product_file where pname=\'\" & l1.Text & \"\' and categry=\'\" & l2.Text & \"\'\"
          Dim dt1 As New DataTable()
            Dim da1 As New SqlDataAdapter(strsql, con)
            da1.Fill(dt1)
            gv.DataSource = dt1
            gv.DataBind()
        Next
当您填充父网格时,请执行以下操作     

要回复问题请先登录注册