首先必須撰寫再RowCreated並且SelectIndexChanged務必使用GridView1.DataBind();
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//記錄當選取實記錄下當時的索引值
int user_select = Convert.ToInt32(GridView1.SelectedIndex.ToString());
// e.Row.RowType == DataControlRowType.Pager代表原本的GridView1已經建置到最後分頁功能的部分,已經建置完畢
if (user_select >= 0 & e.Row.RowType == DataControlRowType.Pager)
{
//前面mycell_0以及mycell_1仍須建置但是不放值,GrideView2放在 mycell_2
GridViewRow myRow = new GridViewRow(user_select + 1, -1, DataControlRowType.DataRow, DataControlRowState.Selected);
TableCell mycell_0 = new TableCell();
mycell_0.BackColor = System.Drawing.Color.RoyalBlue;
TableCell mycell_1 = new TableCell();
mycell_1.BackColor = System.Drawing.Color.RoyalBlue;
TableCell mycell_2 = new TableCell();
mycell_2.BackColor = System.Drawing.Color.RoyalBlue;
myRow.Cells.Add(mycell_0);
myRow.Cells.Add(mycell_1);
if (Page.IsPostBack)
{
UserControl my_uc = new UserControl();
//利用WebControl來建置另一個GridView2
my_uc = (UserControl)LoadControl("Learn_WebUserControl.ascx");
mycell_2.Controls.Add(my_uc);
myRow.Cells.Add(mycell_2);
}
myRow.Visible = true;
//要加二我理解為原本GrideView1的原本那一列再加上GrideView2的Header,因此必須在Index值上加上user_select + 2
GridView1.Controls[0].Controls.AddAt((user_select + 2), myRow);
}