不能将焦点放在CellValueChanged处理程序(.NET)中的DataGridView中的相同单元格上

时间:2021-05-12 22:17:41

In the event handler for CellValueChanged, for a certain condition, I want the focus to stay in the cell and clear its content. What's happening is focus (cursor) is in the next cell when the handler finishes its work. It needs to be in the same cell that caused the event.

在CellValueChanged的事件处理程序中,对于某个条件,我希望焦点保留在单元格中并清除其内容。当处理程序完成其工作时,焦点(光标)在下一个单元格中发生了什么。它需要位于导致事件的同一单元格中。

MyGrid.CellValueChanged -= new DataGridViewCellEventHandler(CellValueChanged);
if (condition)
{
MyGrid.Rows[e.RowIndex].Cells["ColumnName"].Value = "";
MyGrid.CurrentCell = MyGrid["ColumnName", e.RowIndex];
MyGrid.BeginEdit(true);
return;
}

....

1 个解决方案

#1


I think you are looking for CellValidating event instead value changed event. Put that conditional code in this event and if that condition is failed then make e.Cancel = true, which will keep the focus in the same cell.

我认为您正在寻找CellValidating事件而不是值更改事件。将该条件代码放在此事件中,如果该条件失败,则使e.Cancel = true,这将使焦点保持在同一单元格中。

#1


I think you are looking for CellValidating event instead value changed event. Put that conditional code in this event and if that condition is failed then make e.Cancel = true, which will keep the focus in the same cell.

我认为您正在寻找CellValidating事件而不是值更改事件。将该条件代码放在此事件中,如果该条件失败,则使e.Cancel = true,这将使焦点保持在同一单元格中。