我可以DataGridView.EndEdit触发CellValidating事件? [英] Can I make DataGridView.EndEdit trigger the CellValidating event?
问题描述
我用在我的WinForms应用程序中的DataGridView。我的主要目标是使输入键不移动到网格中的下一行。我还是想回车键确认并结束编辑模式。
I'm using a DataGridView in my WinForms application. My main objective is to make the Enter key not move to the next row in the grid. I still want the enter key to validate and end edit mode.
我发现的此FAQ条目和子类的DataGridView覆盖ProcessDialogKey()。如果按下的键是输入,我叫EndEdit中(),否则我叫base.ProcessDialogKey()。
I found this FAQ entry and subclassed DataGridView to override ProcessDialogKey(). If the key pressed is Enter, I call EndEdit(), otherwise I call base.ProcessDialogKey().
它的伟大工程,除了CellValidating事件不被解雇。
It works great, except the CellValidating event isn't fired.
目前,我只是手动调用我的验证逻辑之前,我打电话EndEdit中,但它看起来像我想的东西。
Currently, I'm just manually calling my validation logic before I call EndEdit, but it seems like I'm missing something.
我想我可以打电话OnCellValidating,但随后我会担心我失去了一些其他的事件。我真正想要的是行为就像按与添加禁用一个网格的最后一行进入EndEdit中()的一番风味。
I guess I could call OnCellValidating, but then I'd be worried I'm missing some other event. What I really want is some flavour of EndEdit() that behaves just like pressing enter on the last row of a grid with adding disabled.
推荐答案
CellValidating不会被调用,直到您更改CurrentCell。所以我解决这个kludged的方式是改变CurrentCell,然后切换回当前的。
CellValidating doesn't get called until you change the CurrentCell. So the way I kludged around this was to change the CurrentCell, then switch back to the current one.
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
DataGridViewCell currentCell = CurrentCell;
EndEdit();
CurrentCell = null;
CurrentCell = currentCell;
return true;
}
return base.ProcessDialogKey(keyData);
}
这篇关于我可以DataGridView.EndEdit触发CellValidating事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!