private void dataGridViewX1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { var row = e.RowIndex; if (row < 0) { return; } var column = e.ColumnIndex; // 如果输入的是1,背景变红色 var editValue = this.dataGridViewX1.Rows[row].Cells[column]?.Value.ToString().Trim(); if (editValue == "1") { this.dataGridViewX1.Rows[row].Cells[column].Style.BackColor = Color.Red; } // 如果值的范围不在1~10提示 var editNumValue = 0.0; double.TryParse(editValue, out editNumValue); if (editNumValue < 1 || editNumValue > 10) { MessageBox.Show("请输入正确的分数(1~10)"); this.dataGridViewX1.Rows[row].Cells[column].Value = ""; return; } var sexName = SwitchProjectNamesToSex(this.cbProjectNames.Text); int round = Convert.ToInt32(this.cbRounds.Text); var roundField = string.Empty; if (IsFinal) { roundField = ConvertRoundToDBField(round, true); } else { roundField = ConvertRoundToDBField(round); } var groupName = this.cbGroupNames.Text; var matchId = MatchId; if (RecordByGroup) { var teamShortName = dataGridViewX1.Rows[row].Cells[1].Value.ToString(); var name = dataGridViewX1.Rows[row].Cells[2].Value.ToString(); var scores = new List <string>(); var columnsCount = dataGridViewX1.Rows[row].Cells.Count; var sum = 0.0; for (int i = 0; i < MatchInfo.NumberOneRound; i++) { scores.Add(dataGridViewX1.Rows[row].Cells[i + 3].Value.ToString()); } // 统计所有分数 for (var i = 0; i < columnsCount - 1; i++) { var cellValue = dataGridViewX1.Rows[row].Cells[i].Value.ToString(); var num = 0.0; double.TryParse(cellValue, out num); sum += num; } dataGridViewX1.Rows[row].Cells[columnsCount - 1].Value = sum; if (scores.Count > 0) { AthletesDAL.UpdateScore(matchId, groupName, sexName, roundField, teamShortName, name, string.Join(";", scores)); } } else { var teamShortName = dataGridViewX1.Rows[row].Cells[0].Value.ToString(); var name = dataGridViewX1.Rows[row].Cells[1].Value.ToString(); var scores = new List <string>(); var columnsCount = dataGridViewX1.Rows[row].Cells.Count; var sum = 0.0; for (int i = 0; i < MatchInfo.NumberOneRound; i++) { scores.Add(dataGridViewX1.Rows[row].Cells[i + 2].Value.ToString()); } // 统计所有分数 for (var i = 0; i < columnsCount - 1; i++) { var cellValue = dataGridViewX1.Rows[row].Cells[i].Value.ToString(); var num = 0.0; double.TryParse(cellValue, out num); sum += num; } dataGridViewX1.Rows[row].Cells[columnsCount - 1].Value = sum; if (scores.Count > 0) { AthletesDAL.UpdateScore(matchId, groupName, sexName, roundField, teamShortName, name, string.Join(";", scores)); } } }