如何创建一个嵌套的GridView编辑EF code首先关系? [英] How to create a nested GridView to edit EF Code First relation?

查看:96
本文介绍了如何创建一个嵌套的GridView编辑EF code首先关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经典的父子关系,我想用 ASP来CRUD:GridView控件控制。要CRUD父很容易,但面临的挑战是嵌套 ASP:GridView的 A ASP中:GridView的即能够在孩子有关的工作。

为了使问题更容易,我已经构建了一个例子。请看下面的EF-code:

 公共类语境的DbContext
{
    公共DbSet<动物>动物{搞定;组; }
    公共DbSet<标记和GT;标签{搞定;组; }
}公共类动物
{
    公众诠释AnimalID {搞定;组; }
    公共字符串名称{;组; }
    公共虚拟IEnumerable的<标记和GT;标签{搞定;组; }
}公共类标签
{
    公众诠释标签识别{搞定;组; }
    公共字符串名称{;组; }
}

我使用的是 ASP:GridView的来查看/编辑动物 objectes:

 < ASP:GridView控件=服务器的DataSourceID =animalDataSource的DataKeyNames =AnimalID的AutoGenerateColumns =false的>
    <柱体和GT;
        < ASP:BoundField的数据字段=说明的HeaderText =说明/>
        < ASP:CommandField中ShowCancelButton =真ShowEditButton =真的ShowDeleteButton =真/>
    < /专栏>
< / ASP:GridView的>

数据源势必背后code:

 保护无效DataSource_ContextCreating(对象发件人,EntityDataSourceContextCreatingEventArgs E)
{
        VAR语境=新的上下文();
        e.Context =((IObjectContextAdapter)上下文).ObjectContext; }
}

我想包括嵌套的 ASP:GridView的作为一列添加/删除/修改标签对象beloning到动物我怎么能做到这一点?


解决方案

在绑定列显示指定数据源字段作为文本的价值。通过使用绑定的领域,我们可以将数据直接通过使用标题文本和数据域,而无需使用任何控件绑定。 。该模板列允许HTML标记,Web控件和数据绑定语法的组合。我们可以在模板字段定义我们自己的asp.net控制。所以基本上你转换绑定字段添加到模板列列的模板也配备了一个编辑模板标签,它为您提供比gridview的行的标准编辑更需要......例如,当在编辑模式下把一个下拉列表,在此行中对我来说,从选择 - 可能性是无穷如此


  • 更​​改模板字段去修改


  • 模板添加网格控制领域


  • 添加编辑/删除链接按钮,将其


  • 嵌套表格的属性
  • 去下编辑模板

  • 找到更新,排databounfd事件等

  • 我认为这将有助于


      

    昏暗GRD1作为GridViewRow


     昏暗GV作为GridView控件
            昏暗的L1,L2作为标签
            昏暗的STRSQL作为字符串
            对于每个GRD1在GridView1.Rows
                找父母gridrow的控制
                L1 = grd1.FindControl(L00)
                L2 = grd1.FindControl(L1)
                GV = grd1.FindControl(GV1)
                STRSQL =&AMP选择从那里product_file = PNAMEFILE_NAME l1.Text&安培; '和categry ='与& l2.Text&安培; '
              昏暗DT1作为新的DataTable()
                昏暗DA1作为新的SqlDataAdapter(STRSQL,CON)
                da1.Fill(DT1)
                gv.DataSource = DT1
                gv.DataBind()
            下一个


做这样的事情,当你填写你的父母电网

I've got a classic Parent-Child relation that I would like to CRUD by using asp:GridView controls. To CRUD the parent is easy, but the challenge is to nest a asp:GridView within a asp:GridView that is able to work on the child relation.

To make the problem easier, I've constructed an example. Consider the following EF-code:

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; }
}

I'm using an asp:Gridview to view / edit the Animal objectes:

<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>

The DataSource is bound with code behind:

protected void DataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e) 
{    
        var context = new Context();
        e.Context = ((IObjectContextAdapter)context).ObjectContext; } 
}

I would like to include a nested asp:Gridview as one of the columns to add / remove / edit Tag objects beloning to that Animal. How could I achieve this?

解决方案

The BoundField displays the value of specified DataSource field as text. By using bound field we can bind the data directly by using header text and datafield without using any controls. . The TemplateField allows for a mix of HTML markup, Web controls, and data-binding syntax. We can define our own asp.net controls in template field. so basically you convert a bound field to a template column Template columns also come with a edit template tag which offers you more than the standard editing of that gridview row is desired... e.g when in edit mode put a drop down list in this row for me to select from - possibilities are endless so

  • change to template field go to edit

  • template add grid control to field

  • add Edit/delete link button to it

  • go on the property of nested grid under edit template
  • find update, row databounfd event etc
  • i think this will help

    Dim grd1 As 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
    

do something like this when you fill your parent grid

这篇关于如何创建一个嵌套的GridView编辑EF code首先关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆