Steve McCall
Steve McCall

Reputation: 363

ASP.net Gridview, best way to represent many to many?

I'm working on a product using ASP.net / vb.net.

What I need to do is represent a matrix of locations vs products and select which products are available in which locations by using a grid of checkboxes.

The products are provided via a table (ProductID, ProductName) as are locations (LocationID, LocationName) and I have a Many to Many table (ID, ProductID, LocationID). The issue I'm stuck with is how to create a gridview with dynamic columns and rows based on separate data sources.

Am I being really stupid here? (actually, don't answer that!)

Any help would be greatly appreciated.

Thanks,

Steve

Upvotes: 0

Views: 101

Answers (2)

rohit singh
rohit singh

Reputation: 590

There are two ways to do this :

  1. Use a repeater control

  2. Pivot the table at database end and bind it to gridview normally.

Upvotes: 2

enb081
enb081

Reputation: 4061

You can use nested Gridviews:

<asp:GridView ID="Products" runat="server">
    <Columns>
        <asp:BoundField DataField="ProductId" />
        <asp:BoundField DataField="ProductName" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="Locations" runat="server">
                    <Columns>
                        <asp:BoundField DataField="LocationId" />
                        <asp:BoundField DataField="LocationName" />
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The Products gridview has a TemplateField with a gridview Locations inside.

Upvotes: 1

Related Questions