日期:2014-05-18 浏览次数:20466 次
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> public class office { public string name { get; set; } public string address { get; set; } public List<worker> listworker { get; set; } } public class worker { public string name { get; set; } public string sex { get; set; } public string age { get; set; } } public List<office> GetListOffice() { List<office> ls = new List<office>(); office o = null; worker w = null; Random r = new Random(); for (int i = 0; i < 10; i++) { List<worker> lw = new List<worker>(); for (int j = 1; j < r.Next(4,10); j++) { lw.Add(new worker() { name = "worker" + i.ToString() + "_" + j.ToString(), sex = "A", age = (j * j * i).ToString() }); } o = new office() { name = "officename" + i.ToString(), address = "Ad" + i.ToString(), listworker = lw }; ls.Add(o); } return ls; } protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = this.GetListOffice(); GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { List<worker> lw = DataBinder.Eval(e.Row.DataItem, "listworker") as List<worker>; GridView gv2 = e.Row.FindControl("GridView2") as GridView; gv2.DataSource = lw; gv2.DataBind(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <div style="background-color:Orange;font-weight:bold;font-size:24px">姓名:<%#Eval("name")%> 地址:<%#Eval("address")%> Worker列表如下</div> <asp:GridView ID="GridView2" runat="server" Width="100%"> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>
------解决方案--------------------
上面的数据只是示例数据,你可以换成你的
显示样式根据你的需要自己修改即可
ObjectDataSource 的例子
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x