這聽起來就像你試圖綁定一個分層集合一樣。從你所描述的,我會建議使用ListView而不是GridView。您將在顯示數據方面獲得更多的控制權和靈活性。然而,選擇是你的,下面的代碼可以很容易地修改爲在GridView中使用。
該代碼將產生以下結構:
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();
}
你指的是什麼語言? – 2011-05-25 20:26:28
你能指出你使用的語言嗎? – 2011-05-25 20:29:31
對不起,C#和我正在使用.NET 4.0 – Andy 2011-05-25 20:30:44