简体   繁体   English

将GridView2添加到GridView1行

[英]Add GridView2 into GridView1 row

I have two Grid View gridview1 and gridview2 inside an asp.net page. 我在asp.net页面中有两个Grid View gridview1gridview2 In order to bind data to gridview2 the gridview1 has to bind first because it depends on its values . 为了将数据绑定到gridview2 ,必须先绑定gridview1 ,因为它取决于其values

After finish binding gridview2 i want to add a new row in gridview1 and add the entire gridview2 inside the last row. 完成绑定gridview2我想在gridview1添加新行,并将整个gridview2添加到最后一行中。 So it is not possible to do it onRowDataBound of gridview1 . 因此,不可能在gridview1 onRowDataBound上执行此onRowDataBound

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(). 然后,首先调用BindParentGrid() ,然后调用BindChildGrid().

this link will also help you : http://www.codeproject.com/Articles/22928/GridView-within-GridView 该链接也将帮助您: http : //www.codeproject.com/Articles/22928/GridView-within-GridView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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