我正在研究一个程序DataGridViews.在一个中DatagridView有一个DataGridViewTextBoxColumn,它可以由用户编辑.当用户完成键入数字时,他按下键盘上的ENTER.现在它DataGridView做了所有的Events,毕竟Events,最后一件事就是问题.
一切都完成了,Windows将选择下一个DataGridViewRow,我无法阻止这一点.
我试过了
if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true; // or e.Handled
Run Code Online (Sandbox Code Playgroud)
在我发现的几乎所有事件中.可悲的是,我只能在DataGridViewTextBoxColumn未处于编辑模式时阻止ENTER键.
继续我的方法在编辑时找到了ENTER
添加事件
private void dgr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress += new KeyPressEventHandler(dgr_KeyPress_NumericTester);
}
Run Code Online (Sandbox Code Playgroud)
这是仅接受数字输入的事件.
private void dgr_KeyPress_NumericTester(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8) e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
详细解释:
当用户输入一个具有某些依赖性的Value时,我想给另一个控件提供焦点,因此他用来纠正依赖.
我也尝试过,DependingControl.Focus()但最后一次"输入"将是视图中的最后一件事.
有人知道如何防止这种情况吗?