GridView with a Many to Many Datasource
I am having a problem achieving something. I have a Gridview. In the Gridview will be books. Now, books and authors have a many to many relationship, so you cannot just use an SQLDataSource with a JOIN query, as you will end up with multiple rows for the one book (one for each author).
I figured using the ObjectDataSource would be my best bet.
I created an ObjectDataSource. The SelectMethod for that ObjectDataSource is a method that returns an array of books i.e. Book[] . Within each Book object in that array is a generic list of authors i.e. List<Author>. The challenge is, somehow accessing the properties of those Authors to output in the relevant cells of the GridView. That is where I am stuck. Here is a bit of markup to give you an idea (the 2nd column template is the one to scrutinise):
<asp:GridView ID="titles" runat="server" AllowPaging="true" AllowSorting="true"
AutoGenerateColumns="False" DataSourceID="PubsDS" EnableViewState="False"
OnRowCreated="titles_RowCreated" OnSorting="titles_Sorting"
OnPreRender="titles_PreRender" DataKeyNames="ISBNNr">
<Columns>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:HyperLink id="titleItemLink" runat="server"
NavigateUrl='<%# Eval("ISBNNr", "item.aspx?title_id={0}") %>'
Text='<%# Eval("Title") %>'>
</asp:HyperLink>
</ItemTemplate>
<ItemStyle CssClass="w200" />
<HeaderStyle CssClass="w200" />
<FooterStyle CssClass="w200" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="tester"
NavigateUrl='<%# Eval("Authors") %>'
Text='<%# Eval(Authors) %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ISBNNr" HeaderText="ISBN" SortExpression="ISBNNr" />
<asp:HyperLinkField DataNavigateUrlFields="ISBNNr" DataNavigateUrlFormatString="item.aspx?title_id={0}"
DataTextField="ISBNNr" DataTextFormatString="more information" HeaderText="Item Information" />
</Columns>
</asp:GridView>