//When a student is added, edited or deleted //remove/add them to appropriate StudentList public void OnStudentAltered(object source, StudentEventArgs e) { try { dbUpdateOperation(e); if (e.Operation == "delete") { if (hasStudentPassed(e.SelectedStudent)) { passedStudents.Remove(e.SelectedStudent); passedCounter--; } else { failedStudents.Remove(e.SelectedStudent); } totalCounter--; } else if (e.Operation == "edit") { if (e.SelectedStudent.gradeChanged) { if (hasStudentPassed(e.SelectedStudent)) { failedStudents.Remove(e.SelectedStudent); studentPassed(e.SelectedStudent); } else { passedStudents.Remove(e.SelectedStudent); passedCounter--; studentFailed(e.SelectedStudent); } } } else if (e.Operation == "add") { if (e.SelectedStudent.gradePassed(e.SelectedStudent.FinalGrade)) { studentPassed(e.SelectedStudent); } else { studentFailed(e.SelectedStudent); } totalCounter++; } PercentPassedCalc(totalCounter, passedCounter); backButton.Visibility = Visibility.Hidden; Main.Content = dis1; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
protected virtual void OnStudentAltered(string operation) { EventHandler <StudentEventArgs> localStudentAltered = StudentAltered; if (localStudentAltered != null) { if (operation == "edit") { selectedStudent.FinalGradeCalc(); } StudentEventArgs args = new StudentEventArgs() { SelectedStudent = selectedStudent, Operation = operation }; StudentAltered(this, args); } }
//Depending on add, edit or delete, performs a different query public void dbUpdateOperation(StudentEventArgs op) { switch (op.Operation) { case "add": context.Students.Add(op.SelectedStudent); break; case "edit": //Should just jump to savechanges break; case "delete": context.Students.Remove(op.SelectedStudent); break; default: throw new InvalidOperationException(); } context.SaveChanges(); }