日期:2014-05-18  浏览次数:20466 次

GridView绑定的对象列表中的一个属性是一个对象列表怎么处理
类结构如下:
public static class OfficeAccess{
  public static List<office> GetListOffice() { 
   
  }
}

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

在aspx页面中是这样构成的:
<asp:ObjectDataSource id="srcOffice" TypeName="OfficeAccess" SelectMethod="GetListOffice" runat="server" />
用gridView来显示数据。office类的name,address都好处理,我想知道的是listworker这个属性怎么办呀?

------解决方案--------------------
下面是一个例子
直接宝贝粘贴运行
HTML code
<%@ 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 的例子

HTML code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x