网上也有关于gridview的嵌套方法,感觉不灵活.所以自己写一个.
*.aspx 代码 很简单
<asp:GridView ID="GridView1" runat="server" Width="100%" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>
*.aspx.cs 多余的代码已经去了.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = CreateList("钢铁");
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.Controls.Add(GetGridView(cell.Text));
}
}
}
//第二个GridView
private GridView GetGridView(string Text)
{
GridView gv = new GridView();
gv.ID = string.Concat(Text, "id");
gv.DataSource = CreateList(Text);
gv.DataBind();
gv.HeaderRow.Visible = false;
gv.Font.Size = FontUnit.Parse("12px");
gv.Width = Unit.Parse("100%");
//gv.RowDataBound += event; 再往下嵌套.....
return gv;
}
//记录集
public ICollection CreateList(string Text)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("一季度", typeof(System.String)));
dt.Columns.Add(new DataColumn("二季度", typeof(System.String)));
DataRow dr;
for (int i = 0; i < 10; i++)
{
Random r = new Random();
dr = dt.NewRow();
dr[0] = i.ToString() + "月";
dr[1] = r.Next(99).ToString() + "%";
dt.Rows.Add(dr);
}
return new System.Data.DataView(dt);
}
该方法还可以自己进一步扩展成为多级嵌套