private void GetGradedUnits() { gradedCount = 0; loadedGrades = new List <StudentUnitGrade>(); for (int i = 0; i < DGV_DialogGrade.Rows.Count; i++) { if ((bool)DGV_DialogGrade.Rows[i].Cells["Competent"].FormattedValue) { StudentUnitGrade studentUnitGrade = new StudentUnitGrade() { StudentEmail = email, UnitCode = DGV_DialogGrade.Rows[i].Cells["UnitCode"].Value.ToString(), SubjectId = (int)DGV_DialogGrade.Rows[i].Cells["SubjectId"].Value, CourseCode = courseCode, Year = new DateTime(semesterKey.Year, 1, 1), SecondSemester = semesterKey.Semester.Equals(2), CollegeId = collegeId, Grade = (bool)DGV_DialogGrade.Rows[i].Cells["Competent"].FormattedValue, RPL = (bool)DGV_DialogGrade.Rows[i].Cells["RPL"].FormattedValue, DateGraded = DateTime.Parse(DGV_DialogGrade.Rows[i].Cells["DateGraded"].Value.ToString()).Date }; loadedGrades.Add(studentUnitGrade); gradedCount++; } } WriteResults(); }
private void BTN_DialogGradeAdd_Click(object sender, EventArgs e) { selectedGrades = new List <StudentUnitGrade>(); for (int i = 0; i < DGV_DialogGrade.Rows.Count; i++) { if ((bool)DGV_DialogGrade.Rows[i].Cells["Competent"].FormattedValue) { StudentUnitGrade studentUnitGrade = new StudentUnitGrade() { StudentEmail = email, UnitCode = DGV_DialogGrade.Rows[i].Cells["UnitCode"].Value.ToString(), SubjectId = (int)DGV_DialogGrade.Rows[i].Cells["SubjectId"].Value, CourseCode = courseCode, Year = new DateTime(semesterKey.Year, 1, 1), SecondSemester = semesterKey.Semester.Equals(2), CollegeId = collegeId, Grade = (bool)DGV_DialogGrade.Rows[i].Cells["Competent"].FormattedValue, RPL = (bool)DGV_DialogGrade.Rows[i].Cells["RPL"].FormattedValue, DateGraded = DGV_DialogGrade.Rows[i].Cells["DateGraded"].Value.Equals(DBNull.Value) ? DateTime.Now.Date : DateTime.Parse(DGV_DialogGrade.Rows[i].Cells["DateGraded"].Value.ToString()).Date }; selectedGrades.Add(studentUnitGrade); } } var toAdd = selectedGrades .Select(o => new { o.StudentEmail, o.UnitCode, o.SubjectId, o.CourseCode, o.Year, o.SecondSemester, o.CollegeId, o.Grade, o.RPL, o.DateGraded }) .Except( loadedGrades .Select(o => new { o.StudentEmail, o.UnitCode, o.SubjectId, o.CourseCode, o.Year, o.SecondSemester, o.CollegeId, o.Grade, o.RPL, o.DateGraded }) ); var toDelete = loadedGrades .Select(o => new { o.StudentEmail, o.UnitCode, o.SubjectId, o.CourseCode, o.Year, o.SecondSemester, o.CollegeId, o.Grade, o.RPL, o.DateGraded }) .Except( selectedGrades .Select(o => new { o.StudentEmail, o.UnitCode, o.SubjectId, o.CourseCode, o.Year, o.SecondSemester, o.CollegeId, o.Grade, o.RPL, o.DateGraded }) ); if (toAdd.Count() < 1 && toDelete.Count() < 1) { _ = MessageBox.Show(null, "No modifications have been made.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } List <StudentUnitGrade> a = new List <StudentUnitGrade>(); List <StudentUnitGrade> d = new List <StudentUnitGrade>(); if (toAdd.Count() > 0) { foreach (var item in toAdd) { a.Add(new StudentUnitGrade { StudentEmail = item.StudentEmail, UnitCode = item.UnitCode, SubjectId = item.SubjectId, CourseCode = item.CourseCode, Year = item.Year, SecondSemester = item.SecondSemester, CollegeId = item.CollegeId, Grade = item.Grade, RPL = item.RPL, DateGraded = item.DateGraded }); } } if (toDelete.Count() > 0) { foreach (var item in toDelete) { d.Add(new StudentUnitGrade { StudentEmail = item.StudentEmail, UnitCode = item.UnitCode, SubjectId = item.SubjectId, CourseCode = item.CourseCode, Year = item.Year, SecondSemester = item.SecondSemester, CollegeId = item.CollegeId, Grade = item.Grade, RPL = item.RPL, DateGraded = item.DateGraded }); } } if (cmd.ModifyStudentGrades(a, d)) { // no navigation required } }