【问题标题】:Databind gridview with LINQ使用 LINQ 绑定网格视图
【发布时间】:2011-02-24 20:16:53
【问题描述】:

我有两个数据库表,一个用于网站用户,包含字段“UserID”、“Name”和外键“PageID”。另一个带有“PageID”(这里是主键)和“Url”字段。

我希望能够在 gridview 中使用两个表中的数据显示数据,并且我希望通过 aspx 页面中的数据绑定来实现。

不过,我不知道该怎么做,也找不到任何关于这种特殊情况的好例子。这是我目前所拥有的:

  <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="LinqBinding._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Testing LINQ
    </h2>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSourceUsers" AutoGenerateColumns="false">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="UserID" HeaderText="UserID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="PageID" HeaderText="PageID" />
            <asp:TemplateField HeaderText="Pages">
            <ItemTemplate
                <asp:DropDownList ID="DropDownList1"
                 DataSourceID="LinqDataSourcePages"
                 SelectedValue='<%#Bind("PageID") %>'
                 DataTextField="Url"
                 DataValueField="PageID"
                 runat="server">
                </asp:DropDownList>
            </ItemTemplate>
            </asp:TemplateField>           
        </Columns>
    </asp:GridView>
    <asp:LinqDataSource ID="LinqDataSourcePages" runat="server" 
    ContextTypeName="LinqBinding.UserDataContext" EntityTypeName="" 
        TableName="Pages">
    </asp:LinqDataSource>
    <asp:LinqDataSource ID="LinqDataSourceUsers" runat="server" 
        ContextTypeName="LinqBinding.UserDataContext" EntityTypeName="" 
        TableName="Users">
    </asp:LinqDataSource>
</asp:Content>

但这仅适用于将用户表放入gridview(这不是问题),并且我将页面数据放入下拉列表中,但问题是:我当然将所有页面数据放入在那里,不仅仅是每一行上每个用户的页面。那么,如何在每一行的下拉列表中设置某种“位置”约束,以仅显示该行中用户的页面? (另外,老实说,我不确定我是否得到了正确的外键关系,因为我不太习惯处理关系)。

编辑:

我认为我错误地建立了关系。我不断收到“页面”不作为用户对象的属性存在的消息。而且我想它不能,因为现在的关系是一种方式。所以我试图建立一个多对多的关系。同样,我的数据库知识有点有限,但我添加了一个所谓的“联结表”,其中包含字段 UserID 和 PageID,与其他表的主键相同。虽然我无法在联结表中创建这两个主键(看起来有些人在我见过的示例中拥有......但因为不可能,我猜他们不应该)。无论如何,我从每个表中创建了一个关系,并从中创建了新的 LINQ 类。

但那我该怎么办?我将联结表设置为 Linq 数据源,因为我猜我必须这样做才能访问这两个表,但这不起作用。然后它抱怨该对象上没有 Name 属性。那么如何访问相关表呢?

(顺便说一句:这是我为寻找解决方案而查看的一页:http://www.iaingalloway.com/2015/06/many-to-many-relationships-in-linq-to-sql.html,但首先我不明白他如何修改“订单”类代码隐藏。我只能进入上下文类(我无法按照他的建议右键单击设计视图中的类并查看代码...),并且在那里似乎没有任何方法可以引用联结类 - 在他的情况下是 Order_Details)...我我错过了什么?)

这是我现在拥有的多对多关系:

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="ManyToMany._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Many to many LINQ
    </h2>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1" AutoGenerateColumns="false">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="UserID" HeaderText="UserID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="PageID" HeaderText="PageID" />
        <asp:TemplateField HeaderText="Pages">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1"
             DataSource='<%#Eval("Pages") %>'
             SelectedValue='<%#Bind("PageID") %>'
             DataTextField="Url"
             DataValueField="PageID"
             runat="server">
            </asp:DropDownList>
        </ItemTemplate>
        </asp:TemplateField>           
    </Columns>
</asp:GridView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="ManyToMany.UserPageDataContext"
        EntityTypeName="" TableName="UserPages">
    </asp:LinqDataSource>
</asp:Content>

【问题讨论】:

    标签: linq data-binding datagrid drop-down-menu


    【解决方案1】:

    嗯,看来我自己找到了答案。此页面解释了多对多关系和 LINQ 的问题:http://blogs.msdn.com/b/mitsu/archive/2007/06/21/how-to-implement-a-many-to-many-relationship-using-linq-to-sql.aspx。使用该信息,我可以修改 User 类,使其具有返回 IEnumerable 集合的属性。我将此添加到类 User 中(在 Linq 上下文的代码隐藏中):`

        public IEnumerable<Page> Pages
        {
            get { return UserPages.Select(u => u.Page); }
        }
    

    这样我就可以建立多对多的关系,我可以直接在 aspx 中引用新的 Pages 属性:

        <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ManyToMany._Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView2" runat="server" DataSourceID="LinqDataSourceUsers" AutoGenerateColumns="false">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="UserID" HeaderText="UserID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField HeaderText="Pages">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" DataSource='<%#Eval("Pages") %>' DataTextField="Url"
                                runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:LinqDataSource ID="LinqDataSourceUsers" runat="server" ContextTypeName="ManyToMany.UserPageDBDataContext"
                TableName="Users">
            </asp:LinqDataSource>
        </div>
        </form>
    </body>
    </html>
    

    我希望这可以帮助像我一样一直在苦苦挣扎的其他人!

    【讨论】:

      【解决方案2】:

      尝试将 DropDownList DataSource 绑定到用户对象的 Pages 属性

      <asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSourceUsers" AutoGenerateColumns="false">
          <Columns>
              <asp:CommandField ShowSelectButton="True" />
              <asp:BoundField DataField="UserID" HeaderText="UserID" />
              <asp:BoundField DataField="Name" HeaderText="Name" />
              <asp:BoundField DataField="PageID" HeaderText="PageID" />
              <asp:TemplateField HeaderText="Pages">
              <ItemTemplate
                  <asp:DropDownList ID="DropDownList1"
                   DataSource='<%#Eval("Pages") %>'
                   SelectedValue='<%#Bind("PageID") %>'
                   DataTextField="Url"
                   DataValueField="PageID"
                   runat="server">
                  </asp:DropDownList>
              </ItemTemplate>
              </asp:TemplateField>           
          </Columns>
      </asp:GridView>
      

      【讨论】:

        【解决方案3】:

        我之前在使用 ListView 和 LinqDataSources 时遇到过类似的情况。我发现的唯一解决方案是在 OnRowCreated 或 OnRowDatabound 事件中为 DropDownlist 设置数据源。

        您仍然可以将 LinqDataSource 用于您的 BoundFields,但我怀疑在这种情况下您可能需要在后面的代码中执行 TemplateField。

        代码:

        <asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_OnRowCreated" DataSourceID="LinqDataSource1" AutoGenerateColumns="false">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="UserID" HeaderText="UserID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="PageID" HeaderText="PageID" />
            <asp:TemplateField HeaderText="Pages">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1"
                 DataSource='<%#Eval("Pages") %>'
                 SelectedValue='<%#Bind("PageID") %>'
                 DataTextField="Url"
                 DataValueField="PageID"
                 runat="server">
                </asp:DropDownList>
            </ItemTemplate>
            </asp:TemplateField>           
        </Columns>
        

        C#:

        protected void GridView1_OnRowCreated(object sender, EventArgs e)
        {
           //Create int variable with the UserId value
           //Create new Linq query to be used as datasource.  must get pages and filter by userid.
           //FindControl DropDownList1
           //use the linq query mentioned in the second comment  as the datasource for DropDownList1
           //DataBind DropDownList1
        
           //Example: 
        int userId = /*get userid logic*/;
        var userPages = from t in dc.Pages
                        where t.UserId = userId
                        select t;
        ((DropDownList)/*Find drop down list*/).DataSource = userPages;
        ((DropDownList)/*Find drop down list*/).DataBind()
        }
        

        【讨论】:

        • 对,我实际上已经得到了与此类似的工作(但仅使用代码隐藏)。我假设您的意思是在这里使用一对多关系,对吧?因为否则我会收到我在上面的编辑中提到的消息,即联结对象上不存在属性名称。不过,我真的希望它可以与 aspx 页面中的绑定一起使用,并且我认为它应该使用多对多来完成,因为这很有意义,对吧?一个用户可以点击很多页面,每个页面可以有很多用户……
        • 我可能没有正确理解您,但请查看我的 C# 部分中更新的 cmets,看看是否可以解决问题。我建议您为每个 RowCreated 事件的下拉列表创建一个新的 linq 查询。这有意义吗?
        猜你喜欢
        • 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
        相关资源
        最近更新 更多