網(wǎng)上也有關(guān)于gridview的嵌套方法,感覺(jué)不靈活.所以自己寫一個(gè). *.aspx 代碼 很簡(jiǎn)單 <asp:GridView ID="GridView1" runat="server" Width="100%" OnRowDataBound="GridView1_RowDataBound"> </asp:GridView> *.aspx.cs 多余的代碼已經(jīng)去了. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = CreateList("鋼鐵"); GridView1.DataBind(); } } ///外層gv行綁定 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)); } } } //第二個(gè)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); } |
|
來(lái)自: 悟靜 > 《.net和asp.net》