//Parent GridView
DataTable dt_Parent = new DataTable ();
GridView gv_Parent = new GridView ();
gv_Parent.DataSource = dt_Parent ;
gv_Parent.DataBind();
//Child GridView
DataTable dt_Child = new DataTable ();
GridView gv_Child = new GridView ();
gv_Child.DataSource = dt_Child ;
gv_Child.DataBind();
//新增一行将Child加到Parent中
GridViewRow DetailsRow = new GridViewRow(l_index, -1, DataControlRowType.DataRow, DataControlRowState.Normal); //l_index 要Add的行索引
TableCell HeaderCell = new TableCell();
DetailsRow.Cells.Add(HeaderCell); //在新的一行中加一个空的单元格
TableCell DataCell = new TableCell();
DataCell.Attributes["colspan"] = (gv_Child.Columns.Count - 1).ToString(); //该单元格所占列数(合并)
DataCell.Controls.Add(gv_Child); //在第二个单元格加入Child GridView
DetailsRow.Cells.Add(DataCell);
gv_Parent.Controls[0].Controls.AddAt(l_index + 2, DetailsRow); //将新增的行加入到Parent GridView中
C# ASP.NET GridView中新增一行,嵌套一个GridView
最新推荐文章于 2023-09-10 07:50:48 发布