在Gridview中绑定Gridview

时间:2014-01-22 22:42:08

标签: c# asp.net

我在另一个GridView1中有GridView2。我试图用下面的代码填充它。 GridView4是我用来测试的gridview,它没有嵌套在另一个gridview中。它没有任何问题。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       foreach (GridViewRow gvr in GridView1.Rows)
       { 
          string connectionString = "Data Source=MyConnection String";
          using (SqlConnection test = new SqlConnection(connectionString))
          {
             test.Open();
             SqlCommand cmd = new SqlCommand("SELECT [ID], [CTNum], [PRPNum], [DateEntered], [CurrentRFBDate], [CurrentRFBPRPDate], [CurrentRFPDate], [CurrentRFPPRPDate], [EnteredBy] FROM [PRPDateTracking]", test);

             SqlDataReader reader = cmd.ExecuteReader();
             if (reader.HasRows)
             {
               GridView4.DataSource = reader;
               GridView4.DataBind();
               ((GridView)gvr.FindControl("GridView2")).DataSource = reader;
               ((GridView)gvr.FindControl("GridView2")).DataBind();

             }

        }
    }

}

2 个答案:

答案 0 :(得分:2)

如果父网格视图的每一行都与嵌套的GridView相关联,那么您可以在RowDataBound事件中找到嵌套的网格视图

GridView gvr = (GridView)e.Row.FindControl("GridView2")

答案 1 :(得分:0)

您应该在父网格视图的RowDataBound事件中找到内部(子)gridview,并将值绑定到子gridview。

参考这些链接以了解嵌套网格

http://www.codeproject.com/Articles/685079/Nested-GridView-in-ASP-NET-using-Csharp