[英]datagridview CellEndEdit fires twice
我在datagridview的CellEndEdit
事件中遇到了一些问题。 虽然我了解问题的实质所在,但任何绕过该问题的尝试似乎都失败了。
基本上,在CellEndEdit
事件中,我有一个datagridview,我对数据库进行检查以确保该条目不是重复的。 如果是这样,我会在消息框中提示用户告诉他们他们不能输入重复项,然后以编程方式将值更改回其原始状态/值,并将单元格返回到“编辑”状态。
我的理解是,我以编程方式更改值的事实是事件触发两次的原因。 为了规避,我首先进入事件时设置了一个标志,然后提示+设置+重新编辑,然后将该标志设置为false。 这行不通...任何人都可以告诉我原因或如何实现这一目标?
这是事件代码:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == this.dataGridView1.Columns["Name"].ColumnIndex)
{
if(!this.CellBeingEdited)
{
string NewName = this.dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
//-== DATABASE CODE REMOVED ==-
bool IsDuplicate = ...;
if(IsDuplicate)
{
MessageBox.Show("Cannot have duplicate item names at this level!");
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells["Name"];
this.CellBeingEdited = true;
this.dataGridView1.CurrentCell.Value = this.LastEditedRowName;
this.CellBeingEdited = false;
this.dataGridView1.BeginEdit(false);
return;
}
}
}
}
当我连续编辑一个值时,这段代码不会触发两次:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string test = "test";
if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == test)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
this.dataGridView1.CurrentCell.Value = "not test";
this.dataGridView1.BeginEdit(false);
return;
}
}
也许您在其他地方打电话给该活动?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.