private void GetLoadedSubjects() { loadedSubjects = new List <SubjectInCourseAtSemesterAtCollege>(); for (int i = 0; i < DGV_DialogCourseSubject.Rows.Count; i++) { if ((bool)DGV_DialogCourseSubject.Rows[i].Cells["Select"].FormattedValue) { SubjectInCourseAtSemesterAtCollege subjectTeacher = new SubjectInCourseAtSemesterAtCollege() { Year = new DateTime(semesterKey.Year, 1, 1), SecondSemester = semesterKey.Semester.Equals(2), CollegeId = collegeId, CourseCode = courseCode, SubjectId = (int)DGV_DialogCourseSubject.Rows[i].Cells["SubjectId"].Value, TeacherEmail = DGV_DialogCourseSubject.Rows[i].Cells["TeacherName"].FormattedValue.ToString() }; loadedSubjects.Add(subjectTeacher); } } }
private void BTN_DialogCourseSubjectAdd_Click(object sender, EventArgs e) { if (currentSemester.Key >= semesterKey.Key) { _ = MessageBox.Show(null, "You cannot modify a course after it has commenced.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); DialogResult = DialogResult.Cancel; return; } deselectedCount = 0; selectedSubjects = new List <SubjectInCourseAtSemesterAtCollege>(); for (int i = 0; i < DGV_DialogCourseSubject.Rows.Count; i++) { if ((bool)DGV_DialogCourseSubject.Rows[i].Cells["Select"].FormattedValue) { SubjectInCourseAtSemesterAtCollege subjectTeacher = new SubjectInCourseAtSemesterAtCollege() { Year = new DateTime(semesterKey.Year, 1, 1), SecondSemester = semesterKey.Semester.Equals(2), CollegeId = collegeId, CourseCode = courseCode, SubjectId = (int)DGV_DialogCourseSubject.Rows[i].Cells["SubjectId"].Value, TeacherEmail = DGV_DialogCourseSubject.Rows[i].Cells["TeacherName"].Value.ToString() }; selectedSubjects.Add(subjectTeacher); if (DGV_DialogCourseSubject.Rows[i].Cells["TeacherName"].Value.ToString() == "") { _ = MessageBox.Show(null, "Each Selected Subject must have a Teacher.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } } var toAdd = selectedSubjects .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.TeacherEmail }) .Except( loadedSubjects .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.TeacherEmail }) ); var toDelete = loadedSubjects .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.TeacherEmail }) .Except( selectedSubjects .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.TeacherEmail }) ); if (toAdd.Count() < 1 && toDelete.Count() < 1) { _ = MessageBox.Show(null, "No modifications have been made.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (cmd.CountUnitsInCourse(semesterKey.Key, collegeId, courseCode) > 0) { _ = MessageBox.Show(null, "Unassign all dependent units from this course instance before modifying subjects.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); DialogResult = DialogResult.Cancel; return; } List <SubjectInCourseAtSemesterAtCollege> a = new List <SubjectInCourseAtSemesterAtCollege>(); List <SubjectInCourseAtSemesterAtCollege> d = new List <SubjectInCourseAtSemesterAtCollege>(); if (toAdd.Count() > 0) { foreach (var item in toAdd) { a.Add(new SubjectInCourseAtSemesterAtCollege { Year = item.Year, SecondSemester = item.SecondSemester, CollegeId = item.CollegeId, CourseCode = item.CourseCode, SubjectId = item.SubjectId, TeacherEmail = item.TeacherEmail }); } } if (toDelete.Count() > 0) { foreach (var item in toDelete) { d.Add(new SubjectInCourseAtSemesterAtCollege { Year = item.Year, SecondSemester = item.SecondSemester, CollegeId = item.CollegeId, CourseCode = item.CourseCode, SubjectId = item.SubjectId, TeacherEmail = item.TeacherEmail }); } } if (cmd.ModifyCourseSubject(a, d)) { mainForm.GoToSubjectsInCourses(semesterKey.Key, collegeId, courseCode); } DialogResult = DialogResult.OK; }