DataGridView单元格导致对简单箭头键的验证 [英] DataGridView cell causing validation on simple arrow keys

查看:174
本文介绍了DataGridView单元格导致对简单箭头键的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGridView 为什么通过箭头键简单的导航来增加 CellValidating 事件?我该怎么防止呢?以下是关于 DataGridView 的状态,当它调用 CellValidating 时:

Why does DataGridView raise CellValidating event upon simple navigation through arrow keys? How can I prevent it? Here is the key information about DataGridView state when it calls CellValidating:

EditMode = EditOnKeyStrokeOrF2
IsCurrentCellInEditMode = False
e.FormattedValue = [Same as the current value of the cell]

如您所见,用户没有触发任何编辑操作。他只是按着右箭头键移动通过单元格。一个人不会期望在这一点上发生验证,对吗?

As you can see, user has not triggered any editing operation. He's just moving across the cells by pressing right-arrow key. One would not expect validation to occur at this point, right?

注意:检查调用堆栈显示 KeyDown 事件触发一个 CommitEdit 调用,这又调用 OnCellValidating

Note: Examining call stack shows that KeyDownevent triggers a CommitEdit call, which in turn raises OnCellValidating.

推荐答案

喜欢与否,这是事情想要工作的方式。请参阅 MSDN on CellValidating

Like it or not, this is the way things are desigend to work. See MSDN on CellValidating:


当单元格输入焦点失效时发生内容验证。

Occurs when a cell loses input focus, enabling content validation.

并使其完成 MSDN CellValidated


单元格验证完成后发生。

Occurs after the cell has finished validating.

最直接和可读的解决方案可能是在$ code> CellValidating 事件开始时放置一个这样的行:

The most direct and readable solution is probably to put a line like this at the start of the CellValidating event:

if (!dataGridView1.IsCurrentCellDirty)  return;

设计行为的一个原因可能是实际需要用户输入创建有效的单元格值。不是你的情况,但仍然是一个可以想象的要求。

One reason for the designed behaviour could be the case where a user input is actually needed to create a valid cell value. Not your case, but still a conceivable requirement.

这篇关于DataGridView单元格导致对简单箭头键的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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