We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

master-detail DataGrid's for many-to-many relationship

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? Thanks, Dennis

1 Reply

DM Dennis McCarthy August 15, 2003 04:58 PM UTC

Here's how I got this to work: 1) Read the STUDENTS, COURSES and ENROLLMENTS tables into a DataSet. 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 of "Parent(COURSE_ENROLLMENTS).COURSE_NAME". 4) Bind the master grid to the STUDENTS DataTable in the DataSet. 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. Dennis

Live Chat Icon For mobile
Up arrow icon