紧急求助啊,XDJM们。小弟遇到一个难题,就是在DataGridView控件的单元格的CellEndEdit事件中添加了一个验证单元格值是否重复的功能,并弹出一个对话框。但是当单元格值重复时,该功能却被调用了两次,这是怎么回事啊?是不是输入缓冲区没有被清空呢?我的代码如下:
//验证学号是否重复
string tempXuehao = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
SqlCommand mySqlCommand2 = new SqlCommand();
mySqlCommand2.CommandText = "select * from Grade where 学号='" + tempXuehao + "'";
mySqlCommand2.Connection = con;
con.Open();
if (mySqlCommand2.ExecuteScalar() != null)
{
MessageBox.Show(this,
"此学号已存在,请重新输入!",
"学号重复!",
MessageBoxButtons.OK,
MessageBoxIcon.Error); this.dataGridView1.BeginEdit(true); }
con.Close();
//验证学号是否重复
string tempXuehao = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
SqlCommand mySqlCommand2 = new SqlCommand();
mySqlCommand2.CommandText = "select * from Grade where 学号='" + tempXuehao + "'";
mySqlCommand2.Connection = con;
con.Open();
if (mySqlCommand2.ExecuteScalar() != null)
{
MessageBox.Show(this,
"此学号已存在,请重新输入!",
"学号重复!",
MessageBoxButtons.OK,
MessageBoxIcon.Error); this.dataGridView1.BeginEdit(true); }
con.Close();
private void datagridview1.cellendedit()
{
if(b) b=false;
else
{
//操作
//... b=true; }
}
DataGridView.CellValidating 事件。