如何在Web窗体中的一个GridView中显示来自两个SQL表的数据?

时间:2013-01-14 16:19:27

标签: c# asp.net .net webforms sql-server-express

在我的SQL Server Express中,我有RecipientsMailingListsRecipientMailingListRecipient的ID和MailingList的ID)表。每个收件人都分配到一些邮件列表。我想在GridView中显示所有收件人的信息。我想通过将表从服务器资源管理器拖放到.aspx源代码来实现此目的。但是,我得到的是GridView没有关于MailingLists的任何信息,因为这些信息在其他表中。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RecipientID" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="RecipientID" HeaderText="Recipient ID" ReadOnly="True" SortExpression="RecipientID" />
            <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            <asp:BoundField DataField="MailingLists" HeaderText="Mailing Lists" SortExpression="MailingLists" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewsletterConnectionString1 %>" DeleteCommand="DELETE FROM [Recipients] WHERE [RecipientID] = @RecipientID" InsertCommand="INSERT INTO [Recipients] ([FirstName], [LastName], [City], [Email], [Phone]) VALUES (@FirstName, @LastName, @City, @Email, @Phone)" ProviderName="<%$ ConnectionStrings:NewsletterConnectionString1.ProviderName %>" SelectCommand="SELECT [RecipientID], [FirstName], [LastName], [City], [Email], [Phone] FROM [Recipients]" UpdateCommand="UPDATE [Recipients] SET [FirstName] = @FirstName, [LastName] = @LastName, [City] = @City, [Email] = @Email, [Phone] = @Phone WHERE [RecipientID] = @RecipientID">
        <DeleteParameters>
            <asp:Parameter Name="RecipientID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="RecipientID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

如何创建包含这三个表中所有信息的GridView(或可能的ListView)(收件人的邮件列表名称详细信息)?

更新: 我有这样的查询用作数据源:

SELECT Recipients.RecipientID, Recipients.FirstName, Recipients.LastName, 
Recipients.City, Recipients.Email, Recipients.Phone, MailingLists.Name
FROM Recipients
INNER JOIN RecipientMailingList
ON Recipients.RecipientID = RecipientMailingList.Recipient_RecipientID
INNER JOIN MailingLists ON RecipientMailingList.MailingList_MailingListID = MailingLists.MailingListID

但是,我得到的是具有多个邮件列表的同一用户的不同行。我期望的结果是为每个用户创建一行,并使用逗号分隔的MailingLists列。

1 个答案:

答案 0 :(得分:0)

您可以使用templatefield并调用您的函数。

                <ItemTemplate>
             <%#GetName(Convert.ToInt32(Eval("RecipientsID"))) %>
         </ItemTemplate>
         </asp:TemplateField>

现在你必须在cs文件中编写一个获取int值并返回string的函数。