Advanced Visual Basic 2010 Chapter 7 Programming Challenge 5 Students and Course Lists
In this programming challenge, you will use LINQ statements to display college courses taken by selected students. You will use a CourseRegistration database, which contains tables named Students and Courses.
• The Students table contains the following columns: Id (smallint, primary key), LastName (varchar(30)), Status (smallint), and Major (varchar(5)).
• The Courses table contains the following columns: Id (varchar(10)), Student_Id(smallint), Credits (smallint), and Grade (float). The primary key of the Courses table consists of two combined columns: Id and Student_Id.
Figure 7-21 shows a one-to-many relationship between the Students and Courses tables. You can find the CourseRegistration.mdf database file in the chapter examples folder.
Use a LINQ query to fill a DataGridView with a list of Student objects. When the user selects a student in the grid, display all courses taken by the student in a separate grid. Use another LINQ query to fill the second grid. A sample is shown in Figure 7-22, in which Student 1001 (Charles) was selected by clicking the left side of his row. The grid on the right fills with the list of courses taken by the selected student. Notice that the rightmost column in the Student grid displays a count of the number of courses the student has taken. This column was not in the database, but it is calculated by the LINQ query. We showed how to do such a calculation in Section 7.1.
The two DataGridView controls should be inserted into panels belonging to a Split-Container control. At runtime, the user will drag the divider between the two panels to adjust their size. To format the Grade column in the right-hand grid, set its Default-CellStyle.Format property like this:
dgvCourses.Columns(2).DefaultCellStyle.Format = “n”