private void GetLoadedUnits()
 {
     loadedUnits = new List <UnitInSubjectInCourseAtSemesterAtCollege>();
     for (int i = 0; i < DGV_DialogSubjectUnit.Rows.Count; i++)
     {
         if ((bool)DGV_DialogSubjectUnit.Rows[i].Cells["Select"].FormattedValue)
         {
             UnitInSubjectInCourseAtSemesterAtCollege unitsInSubject = new UnitInSubjectInCourseAtSemesterAtCollege()
             {
                 Year           = new DateTime(semesterKey.Year, 1, 1),
                 SecondSemester = semesterKey.Semester.Equals(2),
                 CollegeId      = collegeId,
                 CourseCode     = courseCode,
                 SubjectId      = (int)DGV_DialogSubjectUnit.Rows[i].Cells["SubjectId"].Value,
                 UnitCode       = DGV_DialogSubjectUnit.Rows[i].Cells["UnitCode"].Value.ToString()
             };
             loadedUnits.Add(unitsInSubject);
         }
     }
 }
        private void BTN_DialogSubjectUnitAdd_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;
            }
            DataGridViewRow dgvRow;

            deselectedCount = 0;
            selectedUnits   = new List <UnitInSubjectInCourseAtSemesterAtCollege>();
            for (int i = 0; i < DGV_DialogSubjectUnit.Rows.Count; i++)
            {
                dgvRow = DGV_DialogSubjectUnit.Rows[i];
                if ((bool)dgvRow.Cells["Select"].FormattedValue)
                {
                    if ((int)dgvRow.Cells["Subject"].Value == 0)
                    {
                        _ = MessageBox.Show(null, "Each Selected Unit must have a Subject.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    UnitInSubjectInCourseAtSemesterAtCollege unitsInSubject = new UnitInSubjectInCourseAtSemesterAtCollege()
                    {
                        Year           = new DateTime(semesterKey.Year, 1, 1),
                        SecondSemester = semesterKey.Semester.Equals(2),
                        CollegeId      = collegeId,
                        CourseCode     = courseCode,
                        SubjectId      = (int)dgvRow.Cells["Subject"].Value,
                        UnitCode       = dgvRow.Cells["UnitCode"].Value.ToString()
                    };
                    selectedUnits.Add(unitsInSubject);
                }
            }
            var toAdd = selectedUnits
                        .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.UnitCode })
                        .Except(
                loadedUnits
                .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.UnitCode })
                );

            var toDelete = loadedUnits
                           .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.UnitCode })
                           .Except(
                selectedUnits
                .Select(o => new { o.Year, o.SecondSemester, o.CollegeId, o.CourseCode, o.SubjectId, o.UnitCode })
                );

            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, 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 <UnitInSubjectInCourseAtSemesterAtCollege> a = new List <UnitInSubjectInCourseAtSemesterAtCollege>();
            List <UnitInSubjectInCourseAtSemesterAtCollege> d = new List <UnitInSubjectInCourseAtSemesterAtCollege>();

            if (toAdd.Count() > 0)
            {
                foreach (var item in toAdd)
                {
                    a.Add(new UnitInSubjectInCourseAtSemesterAtCollege {
                        Year           = item.Year,
                        SecondSemester = item.SecondSemester,
                        CollegeId      = item.CollegeId,
                        CourseCode     = item.CourseCode,
                        SubjectId      = item.SubjectId,
                        UnitCode       = item.UnitCode
                    });
                }
            }
            if (toDelete.Count() > 0)
            {
                foreach (var item in toDelete)
                {
                    d.Add(new UnitInSubjectInCourseAtSemesterAtCollege {
                        Year           = item.Year,
                        SecondSemester = item.SecondSemester,
                        CollegeId      = item.CollegeId,
                        CourseCode     = item.CourseCode,
                        SubjectId      = item.SubjectId,
                        UnitCode       = item.UnitCode
                    });
                }
            }
            if (cmd.ModifySubjectUnit(a, d))
            {
                mainForm.GoToUnitsInSubjects(semesterKey.Key, collegeId, courseCode);
            }

            DialogResult = DialogResult.OK;
        }