选课程序,要求如果所选课程与课表中已有课程重复,则显示选课失败,如果没有重复选课就显示选课成功。目前的问题是不管有没有重复选课都显示选课失败,代码如下 private void SelectCourse() { bool state = false; if (TestSelectRow(dataGridView_Course)) { DialogResult dr = MessageBox.Show("Do you really want to Select this course \ninto your course schedule?" , "Waring", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { foreach (DataGridViewRow row in dataGridView_Course.Rows) { if ((String)row.Cells[dataGridView_Course.Columns["CourseNo"].Index].Value == (String)dataGridView_Course.SelectedRows[0].Cells[0].Value) { state = true; } } if (!state) { String[] Strings = new String[] { CurrentUser.UserName, (String)dataGridView_Course.SelectedRows[0].Cells[0].Value }; String QueryString = User.GetQueryString(CurrentUser, QueryType.ScoreInsert, Strings); state = ((Student)CurrentUser).Insert(QueryString, QueryType.ScoreInsert, TSDB); if (state) { MessageBox.Show("Select the course into your schedule Successfully\n" + "You can see a new record in your Score.Info" , "Success", MessageBoxButtons.OK); } else { MessageBox.Show("Select the course into your schedule Failed" , "Failed", MessageBoxButtons.OK); } } else { MessageBox.Show("Select the course into your schedule Failed\n" + "Maybe causes:\nYou have the course in your schedule" , "Failed", MessageBoxButtons.OK); } LoadDataGrid(GridViewType.Score); } } }