2012-09-05 78 views
0

我試圖讓這樣的事情:主詳細的GridView asp.net C#

Master detail gridview

但給我的錯誤在這條線上從CS文件時,我在GridView點擊查看詳細信息:

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

我嘗試當我單擊顯示詳細信息以查看ID_Proba =從其他表中的名稱從第一個表。

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

回答

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>