简体   繁体   中英

Add GridView2 into GridView1 row

I have two Grid View gridview1 and gridview2 inside an asp.net page. In order to bind data to gridview2 the gridview1 has to bind first because it depends on its values .

After finish binding gridview2 i want to add a new row in gridview1 and add the entire gridview2 inside the last row. So it is not possible to do it onRowDataBound of gridview1 .

Is there a way to do that?.

I tried the following but is not working

 grdQuestions.DataSource = LoadQuestions();
        grdQuestions.DataBind();

   grdAnswers.DataSource = LoadAnswer(Convert.ToInt32(grdQuestions.Rows[0].Cells[0].Text));
   //GridView2.DataSource = LoadAnswer(2);
   grdQuestions.Rows[0].Cells[0].Controls.Add(grdAnswers);
   grdAnswers.DataBind();
   grdQuestions.DataBind();

You can do it like below :

First create function that bind parent grid.

 private void BindParentGrid()
 {
    //Get data from database in dataset.
      ParentGrid.DataSource = dataset;
      ParentGrid.DataBind();

 }

Then create function that bind child grid.

private void BindChildGrid()
{                       
      //get data from database in dataset2.
       ((GridView)ParentGrid.Rows[i].Cells[1].Controls[1]).DataSource = dataset2;
       ((GridView)ParentGrid.Rows[i].Cells[1].Controls[1]).DataBind();
}

Then first call BindParentGrid() and then call BindChildGrid().

this link will also help you : http://www.codeproject.com/Articles/22928/GridView-within-GridView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM