2011-05-25 59 views
1
Class Section 
    List<Group> groupList 

Class Group 
    List<Products> productList 
    Group ID 
    Group Name 

Class Product 
    Product ID 
    Product Name 
    Product Details 

內的集合上使用C#,.NET 4.0,如何綁定這個數據到GridView讓我有這種格式GridView的 - 綁定集合

G1_Name 
     P1_Name P1_Details 
     P2_Name P2_Details 
G2_Name 
     P3_Name P3_Details 
     P4_Name P4_Details 

感謝

+0

你指的是什麼語言? – 2011-05-25 20:26:28

+0

你能指出你使用的語言嗎? – 2011-05-25 20:29:31

+0

對不起,C#和我正在使用.NET 4.0 – Andy 2011-05-25 20:30:44

回答

0

這聽起來就像你試圖綁定一個分層集合一樣。從你所描述的,我會建議使用ListView而不是GridView。您將在顯示數據方面獲得更多的控制權和靈活性。然而,選擇是你的,下面的代碼可以很容易地修改爲在GridView中使用。

該代碼將產生以下結構:

enter image description here

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(); 
    } 
1

一個搜索解決方案N(許多可能的):

  • 一個GridView綁定到羣組

  • 與包含數據綁定列表控制一個TemplateField的列表數據(例如RepeaterDataList,或埃文另一GridView)。

  • 在數據綁定列表控件的DataSource屬性綁定到Group.ProductList屬性:

<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>