选择gridview,使用嵌套gridview填充gridview。不起作用。救命

时间:2011-12-20 13:32:04

标签: asp.net gridview

我有3个网格视图

第一个gridview启用选择,如果有任何数据,将填充第二个gridview。

而第三个gridview嵌套在第二个gridview内。

但是,第3个gridview永远不会显示任何单个数据.. 我错过了什么?

如果第二个gridview填充在页面加载上,我可以实现此目的。 这是代码 我在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>

1 个答案:

答案 0 :(得分:0)

您必须在AutoPostBack="True"上设置GridView1,以便在用户选择一行时回发。但我不熟悉SqlDataSource。所以我会在GridView2处理程序中设置SelectedIndexChanged的DataSource属性,然后在DataBind处设置它。

如果要在绑定GridView2时将数据绑定到内部网格,则应该处理GridView2的{​​{3}},找到内部网格,设置数据源和数据绑定它

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
  }
}