My database contains a STUDENTS table, a COURSES table, and an ENROLLMENTS table. The ENROLLMENTS table represents the many-to-many relationship between students and courses.
I would like to have a master-detail pair of DataGrid's. The master grid contains students, and the detail grid contains the courses in which the selected student is enrolled.
Master-detail grids are easy when there is a parent-child relationship between two tables. However, in my case the parent child relationship is between the STUDENTS and ENROLLMENTS tables (and the COURSES and ENROLLMENTS).
How can I get a pair of DataGrid's to act as master-detail for this many-to-many relationship?
Here's how I got this to work:
1) Read the STUDENTS, COURSES and ENROLLMENTS tables into
2) Add two DataRelations to the DataSet. One has STUDENTS
as the parent and ENROLLMENTS as the child. The other has
COURSES as the parent and ENROLLMENTS as the child.
3) Add computed DataColumn's to the ENROLLMENTS DataTable
in the DataSet. These columns correspond to columns in
the COURSES table. Their expression is along the lines
4) Bind the master grid to the STUDENTS DataTable in the
5) Bind the detail grid to the ENROLLMENTS DataTable in
the DataSet. It will appear as if the detail grid is
bound to the COURSES table, since the ENROLLMENTS
DataTable contains the COURSES columns.
The master grid will display all students. The detail
grid will display only the courses in which the student
(row selected in the master grid) is enrolled.