user1292656
user1292656

Reputation: 2560

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();

Upvotes: 0

Views: 482

Answers (1)

user1711092
user1711092

Reputation:

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

Upvotes: 4

Related Questions