日期:2014-05-18  浏览次数:20776 次

我的CellEndEdit事件处理程序怎么被调用了两次啊?
紧急求助啊,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();


------解决方案--------------------
为什么要在CellEndEdit事件里面写呢,你可以在CellValueChanged事件里面写,效果一样!
------解决方案--------------------
//this.dataGridView1.BeginEdit(true);
------解决方案--------------------
你查出两个等的,当然提示两次。。
第一次它本身也相当啊,你得排除
------解决方案--------------------
bool b=false;
private void datagridview1.cellendedit()
{
if(b) b=false;
else
{
//操作
//...

b=true;

}


}

------解决方案--------------------
DataGridView的单元格验证可以使用这个事件:
DataGridView.CellValidating 事件。
------解决方案--------------------
探讨
//this.dataGridView1.BeginEdit(true);