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,以便我有这种格式
G1_Name
P1_Name P1_Details
P2_Name P2_Details
G2_Name
P3_Name P3_Details
P4_Name P4_Details
由于
答案 0 :(得分:1)
一种解决方案(很多可能):
GridView,它是绑定到组列表
包含一个包含数据绑定列表控件的TemplateField(例如Repeater
,DataList
或另一个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>
答案 1 :(得分:0)
听起来你正试图绑定一个分层集合。根据您的描述,我建议使用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();
}