C#、WinForms。
也许这是一个愚蠢而微不足道的问题,但我无法摆脱!我有DataGridView14 列。我检查第 1 列中每一行的值是否与第 2 列中前一行的值相同。如果是,MessageBox则会出现 a 通知我...并且我想将焦点移至其中存在的单元格是刚刚输入的双精度值。因此我写了这段代码:
private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs cella)
{
if (cella.RowIndex > 0 && cella.ColumnIndex == 1)
{
var PrevCell = DataGridView1.Rows[cella.RowIndex - 1].Cells[2].Value.ToString();
if (DataGridView1.Rows[cella.RowIndex].Cells[cella.ColumnIndex].Value.ToString() == PrevCell)
{
MessageBox.Show("Amount already exists. Change the current value or the previous occurrence", "Double value, already inserted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
DataGridView1.CurrentCell = DataGridView1.Rows[cella.RowIndex].Cells[cella.ColumnIndex];
DataGridView1.BeginEdit(true);
//only a test:
//return;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
而且CurrentCell效果很好。问题是,CellEndEdit当我按下Tab按键移动到下一个单元格(或者用鼠标单击到下一个单元格)时,这种控制是通过事件执行的,因此,即使将BeginEdit我放在正确的单元格上,当我编辑该值时,一旦再次按 Tab,它就会将更改后的值移动到下一个单元格中。看来在显示 MessageBox …