DataGridView的细胞只有验证时,“Enter”键是pressed [英] DataGridView Cell Validating only when 'Enter' is pressed

查看:108
本文介绍了DataGridView的细胞只有验证时,“Enter”键是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证,并承诺在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.

这篇关于DataGridView的细胞只有验证时,“Enter”键是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆