2011-12-20 94 views
1

我有3個gridviews選中gridview,用嵌套的gridview填充gridview。不工作。 helppp

1st gridview啓用選擇,如果它有任何數據將填充第二個gridview。

而第三個gridview嵌套在第二個gridview中。

但是,第三個gridview永遠不會顯示任何單個數據.. 我在想什麼?

我可以實現這一點,如果第二個gridview填充頁面加載。 這裏是代碼 IM在asp.net C#

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="SurveyID" DataSourceID="SqlDataSource1"> 
     <Columns> 
      <asp:BoundField DataField="SurveyID" HeaderText="SurveyID" 
       SortExpression="SurveyID" /> 
      <asp:BoundField DataField="SurveyTitle" HeaderText="SurveyTitle" 
       SortExpression="SurveyTitle" /> 
      <asp:CommandField ShowSelectButton="True" /> 
     </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
     SelectCommand="SELECT [SurveyID], [SurveyTitle] FROM [Survey]"> 
    </asp:SqlDataSource> 
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="QuestionID" DataSourceID="SqlDataSource2"> 
     <Columns> 
      <asp:BoundField DataField="QuestionID" HeaderText="QuestionID" 
       InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" /> 
      <asp:BoundField DataField="QuestionTitle" HeaderText="QuestionTitle" 
       SortExpression="QuestionTitle" /> 
      <asp:BoundField DataField="AnswerType" HeaderText="AnswerType" 
       SortExpression="AnswerType" /> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
         DataSourceID="SqlDataSource1" DataKeyNames="ResultID"> 
         <Columns> 
          <asp:BoundField DataField="ResultID" HeaderText="ResultID" 
           SortExpression="ResultID" InsertVisible="False" ReadOnly="True" /> 
          <asp:BoundField DataField="QuestionID" HeaderText="QuestionID" 
           SortExpression="QuestionID" /> 
          <asp:BoundField DataField="AnswerValue" HeaderText="AnswerValue" 
           SortExpression="AnswerValue" /> 
         </Columns> 
        </asp:GridView> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
         SelectCommand="SELECT * FROM [Results] WHERE ([QuestionID] = @QuestionID)"> 
         <SelectParameters> 
          <asp:Parameter Name="QuestionID" Type="Int32" /> 
         </SelectParameters> 
        </asp:SqlDataSource> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
     SelectCommand="SELECT [QuestionID], [QuestionTitle], [AnswerType] FROM [Question] WHERE ([SurveyID] = @SurveyID)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="GridView1" Name="SurveyID" 
       PropertyName="SelectedValue" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
+0

顯示你已經嘗試過。 http://stackoverflow.com/faq這是VB.NET或C#? – 2011-12-20 13:36:37

+0

已更新。是.net c# – 2011-12-20 13:43:50

回答

0

你必須設置AutoPostBack="True"GridView1,因此,如果用戶選擇的行回發。但我不熟悉SqlDataSource。所以我會在SelectedIndexChanged處理程序中設置GridView2的DataSource屬性,然後DataBind它。

如果您想在GridView2綁定到數據綁定到內的網格,你應該處理的GridView2RowDataBound event,發現內網有,設置它的數據源和數據綁定它。

void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
    var rowView = (DataRowView)e.Row.DataItem; 
    var GridView3 = (GridView)e.Row.FindControl("GridView3"); 
    //set it's DataSource according to this row's DataItem 
    } 
} 
+0

我會給它一個鏡頭。然而,只是把autopostback真實不會影響,因爲它已經回發時,你點擊鏈接按鈕。 – 2011-12-20 15:07:54