I want to illustrate how to implement nested GridViews. I will use the Authors
table of Pubs
database. If you recall, Pubs
database is part of Microsoft SQL Server samples. We have a parent grid and a child grid. The parent grid, AuthorGrid
displays last name of authors. The child grid, DetailGrid
, displays more details for the selected author.
<asp:GridView ID="AuthorGrid" runat="server" DataSourceID="AuthorSql" AutoGenerateColumns="false" DataKeyNames="au_lname" OnSelectedIndexChanged="AuthorGrid_SelectedIndexChanged"> <Columns> <asp:BoundField HeaderText="Author" DataField="au_lname" /> <asp:TemplateField> <ItemTemplate> <asp:GridView ID="DetailGrid" runat="server" OnRowCommand="DetailGrid_OK" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="First name" DataField="au_fname" /> <asp:ButtonField CommandName="OK" Text="OK" /> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowSelectButton="true" SelectText="Show" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="AuthorSql" runat="server" ConnectionString = "<%$ ConnectionStrings:Pubs %>" SelectCommand="SELECT au_lname FROM Authors" ></asp:SqlDataSource> <asp:SqlDataSource ID="DetailSql" runat="server" DataSourceMode="DataSet" ConnectionString = "<%$ ConnectionStrings:Pubs %>" SelectCommand="SELECT au_fname FROM Authors WHERE au_lname=@lname" > <SelectParameters> <asp:Parameter Type="String" Name="lname" /> </SelectParameters> </asp:SqlDataSource>
AuthorGrid
is bound to a data source.…