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.

AuthorGrid is bound to a data source. The SqlDataSource, AuthorSql, has tabular data with list of author names. The child grid, DetailGrid is not bound. DetailGrid binds to a data source when we select an author in the parent grid. The OnSelectedIndexChanged event handler of the parent grid is shown below.

We retrieve the last name from the parent grid selection. Pass the last name as an argument to the data source. Retrieve the child grid from the selected row of the parent grid. Bind the child grid to the data source. That should populate the DetailGrid or child grid.

The DetailGrid has an OK button. Click the button. And the DetailGrid disappears. The code for the click event handler is shown below.

On clicking OK button, we bind the child grid to an empty data source.

In this example, we have a trivial master-detail or parent-child nested GridView. The parent grid displays last names of authors. And the child grid displays a list of first names of authors who share the same last name. Though the example is trivial, we have all the building blocks to build nested views.

Implementing Master-Detail views with nested GridView controls
Tagged on: