0

我正在尝试做这样的事情:

主细节网格视图

但是当我单击 GridView 中的显示详细信息时,在 cs 文件的这一行中给我错误:

gvRow.FindControl("GridView2").Visible = true;

当我单击显示详细信息以查看 ID_Proba 为 = 到第一个表中的 ID 的其他表中的名称时,我正在尝试。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField 
        DataField="id" 
        HeaderText="id" 
        ReadOnly="True" 
        SortExpression="id" InsertVisible="False" />
        <asp:BoundField 
        DataField="name" 
        HeaderText="name" 
        SortExpression="name" />
        <asp:CommandField ShowSelectButton="True" 
              SelectText="Show Details"/>
    </Columns>
    <EmptyDataTemplate>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
            <Columns>
                <asp:BoundField DataField="id" 
                                HeaderText="id" 
                                SortExpression="id" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="id_proba" 
                                HeaderText="id_proba" 
                                SortExpression="id_proba" />
                <asp:BoundField DataField="name" 
                                HeaderText="name" 
                                SortExpression="name" />
            </Columns>
        </asp:GridView>
    </EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = @id_proba)">
    <SelectParameters>
        <asp:Parameter Name="id_proba" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
</form>

这是cs文件:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow gvRow in GridView1.Rows)
    {
        gvRow.FindControl("GridView2").Visible = true;
    }
    SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
    GridView1.SelectedRow.FindControl("GridView2").Visible = true;    
}
4

1 回答 1

0

您需要将“GridView2”取出EmptyDataTemplate并将其放入TemplateField. 当您有数据时,EmptyDataTemplate根本不会创建,并且每行中也没有网格,因为此模板适用于整个网格。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField 
        DataField="id" 
        HeaderText="id" 
        ReadOnly="True" 
        SortExpression="id" InsertVisible="False" />
        <asp:BoundField 
        DataField="name" 
        HeaderText="name" 
        SortExpression="name" />
        <asp:CommandField ShowSelectButton="True" 
              SelectText="Show Details"/>
<asp:TemplateField>
    <ItemTemplate>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
            <Columns>
                <asp:BoundField DataField="id" 
                                HeaderText="id" 
                                SortExpression="id" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="id_proba" 
                                HeaderText="id_proba" 
                                SortExpression="id_proba" />
                <asp:BoundField DataField="name" 
                                HeaderText="name" 
                                SortExpression="name" />
            </Columns>
        </asp:GridView>
    </ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = @id_proba)">
    <SelectParameters>
        <asp:Parameter Name="id_proba" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
</form>
于 2012-09-05T21:57:35.567 回答