开发者

Gridview - Binding a collection within a Collection

Class Section
    List<Group> groupList

Class Group
   List<Products> productList
   Group ID
   Group开发者_开发问答 Name

Class Product
   Product ID
   Product Name
   Product Details

Using C#, .NET 4.0, How do I bind this data to the GridView so that I have this format

G1_Name
       P1_Name   P1_Details
       P2_Name   P2_Details
G2_Name
       P3_Name   P3_Details
       P4_Name   P4_Details

Thanks


One solution (of many possible):

  • A GridView that is data bound to a list of Groups

  • with a TemplateField that contains a data-bound list control (e.g.Repeater, DataList, or evan another GridView).

  • where the data-bound list control's DataSource property is bound to the Group.ProductList property:

.

<asp:GridView ...>
    <Columns>
        <asp:TemplateField ...>
            <ItemTemplate>
                <asp:Repeater ... DataSource='<%# Eval("ProductList")) %>'>
                    <ItemTemplate>
                         ... controls bound to Product properties
                     </ItemTemplate>
                 </asp:Repeater>
             </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


It sounds like you're trying to bind a hierarchical collection. From what you describe, I would recommend using a ListView instead of a GridView. You'll get a lot more control and flexibility over the display of your data. However, the choice is yours and the code below can be easily modified for use in a GridView.

The code will produce the following structure:

Gridview - Binding a collection within a Collection

ASPX

<asp:ListView runat="server" ID="listView">
        <LayoutTemplate>
            <ul><asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder></ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li><%# Eval("GroupName") %>
                <asp:ListView runat="server" DataSource='<%# Eval("productList")%>'>
                    <LayoutTemplate>
                        <ul><asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder></ul>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <li><%# Eval("ProductName") %> - Details: <%# Eval("ProductDetails") %></li>
                    </ItemTemplate>
                </asp:ListView>
            </li>
        </ItemTemplate>
    </asp:ListView>

C#

        public class Section
        {
            public List<Group> groupList { get; set; }
        }

        public class Group
        {
            public List<Product> productList { get; set; }
            public int GroupID { get; set; }
            public string GroupName { get; set; }
        }

        public class Product
        {
            public int ProductID { get; set; }
            public string ProductName { get; set; }
            public string ProductDetails { get; set; }
        }

        Section section = new Section { groupList = new List<Group>() };

        Group group = new Group { GroupID = 1, GroupName = "Fruits", productList = new List<Product>() };
        Product product = new Product { ProductID = 1, ProductName = "Apples", ProductDetails = "On Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 2, ProductName = "Oranges", ProductDetails = "Not on Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 3, ProductName = "Pears", ProductDetails = "Big Spender Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 4, ProductName = "Grapes", ProductDetails = "Not on Sale" };
        group.productList.Add(product);

        section.groupList.Add(group);

        group = new Group { GroupID = 2, GroupName = "Vegetables", productList = new List<Product>() };
        product = new Product { ProductID = 5, ProductName = "Carrots", ProductDetails = "Not on Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 6, ProductName = "Celery", ProductDetails = "Last week only" };
        group.productList.Add(product);
        product = new Product { ProductID = 7, ProductName = "Eggplant", ProductDetails = "Big Spender Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 8, ProductName = "Oniones", ProductDetails = "N/A" };
        group.productList.Add(product);

        section.groupList.Add(group);

        group = new Group { GroupID = 3, GroupName = "Meat", productList = new List<Product>() };
        product = new Product { ProductID = 9, ProductName = "Beef", ProductDetails = "On Sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 10, ProductName = "Pork", ProductDetails = "Back for two weeks" };
        group.productList.Add(product);
        product = new Product { ProductID = 11, ProductName = "Chicken", ProductDetails = "On sale" };
        group.productList.Add(product);
        product = new Product { ProductID = 12, ProductName = "Turkey", ProductDetails = "Going fast" };
        group.productList.Add(product);

        section.groupList.Add(group);

        listView.DataSource = section.groupList;
        listView.DataBind();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜