DataGridView的细胞只有验证时,“Enter”键是pressed细胞、DataGridView、pressed、Enter

由网友(荒城凉梦)分享简介:我想验证,并承诺在DataGridViewCell的输入值的仅当用户presses的Enter键。I want to validate and commit the value entered in the DataGridViewCell ONLY when the user presses the 'Enter'...

我想验证,并承诺在DataGridViewCell的输入值的仅当用户presses的Enter键。

I want to validate and commit the value entered in the DataGridViewCell ONLY when the user presses the 'Enter' key.

如果用户presses其他任何按键或鼠标键(箭头键,$ P $用鼠标pssing不同的细胞......),我想要的行为是相似的ESC键:移动焦点转移到新的小区,并恢复已编辑的单元格的值到previous值。

If the users presses any other key or mouse button (Arrow keys, Pressing a different cell using the mouse...), I want the behavior to be similar to the 'ESC' key: Move the focus to the new cell and revert the edited cell value to its previous value.

推荐答案

下面可能会奏效。

处理的CellValidating并取消任何改变,除非最后的关键pressed了进去。

Handle the CellValidating and cancel any changes unless the last key pressed was enter.

编辑:

子类的DataGridView控制自己的类,并添加以下code到它:

Subclass the DataGridView control with your own class and add the following code to it:

  private bool m_lastWasEnter = false;
  public bool LastWasEnter()
  {
     return m_lastWasEnter;
  }
  protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
  {
     m_lastWasEnter = keyData == Keys.Return;
     return base.ProcessDialogKey(keyData);
  }

然后,你可以只添加以下到CellValidating事件的处理程序:

Then you could just add the following to the handler of the CellValidating event:

e.Cancel = !instanceOfYourControl.LastWasEnter();

这不会给你你想要什么,但应确保它只会停止编辑,如果用户pressed至少进入,你应该能够对其进行修改,以获得您的具体要求是满足。

This won't give you exactly what you want but should make sure that it'll only stop the editing if the user pressed enter at least and you should be able to modify it to get your exact requirements to be met.

阅读全文

相关推荐

最新文章