2010-10-09 229 views
1

我有一個像我想在另一個網格視圖內使用網格視圖的概念。這是必需的,因爲我想用外鍵顯示有多條相關記錄的記錄。如何在網格視圖中使用網格視圖?

例如,我想要一個輸出,例如我有一個表的課程名稱和另一個表包含每門課程的主題。

在輸出中,我希望每個課程名稱在所有課程列表中顯示其主題下列格式的結果?

我該怎麼做

回答

4

要將網格代碼中顯示網格

每一行是綁定到父網格的數據,甚至是一個OnRowDataBound t被解僱。使用OnRowDataBound事件來捕獲它

OnRowDataBound="gridViewParticipant_RowDataBound" 

protectedvoid gridViewParent_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
GridView gridViewChild = (GridView)e.Row.FindControl("gridViewChild "); 
string participantID = ((DataRowView)e.Row.DataItem)["ParticipantID"].ToString(); 
if (!string.IsNullOrEmpty(participantID)) 
{ 
gridViewChild .DataSource = SOURCE;//load data from database based on foriegn key, make sure that you should select that foreign key field in to your data by which you bind parent gridview, i giving here for example ParticipantID 
gridViewChild .DataBind(); 
} 
else 
{ 
gridViewChild .Visible = false; 
} 
} 
}