private void GradeGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (isAssignmentColumn(e.ColumnIndex)) { StudentViewModel student = (StudentViewModel)this.Rows[e.RowIndex].DataBoundItem; AssignmentViewModel assignment = (AssignmentViewModel)this.Columns[e.ColumnIndex].Tag; if (assignment.Grades.ContainsKey(student.Id)) { if (this[e.ColumnIndex, e.RowIndex].IsInEditMode) { this.Columns[e.ColumnIndex].DefaultCellStyle.Format = ""; e.Value = assignment.Grades[student.Id].Points; } else { this.Columns[e.ColumnIndex].DefaultCellStyle.Format = "P"; double gradePercentage = assignment.CalculateGradePercentage(student); e.Value = gradePercentage; e.CellStyle.BackColor = GradeColorCode.getColor(gradePercentage); } } } else if (this.Columns["totalGradeColumn"].Index == e.ColumnIndex) { StudentViewModel student = (StudentViewModel)this.Rows[e.RowIndex].DataBoundItem; double gradePercentage = Course.CalculateGradePercentage(student); e.Value = gradePercentage; e.CellStyle.BackColor = GradeColorCode.getColor(gradePercentage); } }