DataGridView在Cell编辑状态响应回车键下的KeyPress/KeyDown/KeyUp事件
2014-04-24 19:46
459 查看
我们知道由于DataGridView的单元格DataGridCell处于编辑的时候,当你按Enter键,那么DataGridView是不会激发KewPress/KeyDown/KeyUp这些事件的,因为这个时候的DataGridView是一个容器。
如果我们需要做一些事情,比如在DataGridCell中输入值后需要对其验证,如这位朋友遇到的这个问题:
http://social.msdn.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/2cc22c92-4771-4af4-89cc-c036cd8b556c 他的需求是:
在DataGridView 单元格编辑状态时
按回车时 判断这个单元格的内容是否确定
如果正确光标进入下一行对应的列的单元格中
如果不正确光标还是停留本单元格中
我们无法直接在DataGridView的KeyPress事件中做处理,原因上面已经说明,也无法使用CellEndEdit这个事件,因为这个事件不一定是通过Enter来触发的,直接鼠标移动到其他单元格也会的,因此我们需要修改一下:
建一个类:
然后将这个控件拖到窗体中 添加KeyPress事件
当然为了保证用户直接将鼠标移动到新的单元格也能做数据的核对,我们还需要添加对这个控件的CellEndEdit事件的处理:
这样无论是用户按的Enter键还是直接移动鼠标就都会做相应的验证了。
为了使得代码便于维护最好将判断校验代码单独写到一个方法,供这个两个事件处理调用。
转载自:http://hi.baidu.com/topkaze/item/40e83c0c46dc7519acdc701f
如果我们需要做一些事情,比如在DataGridCell中输入值后需要对其验证,如这位朋友遇到的这个问题:
http://social.msdn.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/2cc22c92-4771-4af4-89cc-c036cd8b556c 他的需求是:
在DataGridView 单元格编辑状态时
按回车时 判断这个单元格的内容是否确定
如果正确光标进入下一行对应的列的单元格中
如果不正确光标还是停留本单元格中
我们无法直接在DataGridView的KeyPress事件中做处理,原因上面已经说明,也无法使用CellEndEdit这个事件,因为这个事件不一定是通过Enter来触发的,直接鼠标移动到其他单元格也会的,因此我们需要修改一下:
建一个类:
namespace WindowsFormsApplication { public sealed class MyDataGridView : DataGridView { protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Enter) { this.OnKeyPress(new KeyPressEventArgs('r')); return true; } else return base.ProcessCmdKey(ref msg, keyData); } } }
然后将这个控件拖到窗体中 添加KeyPress事件
private void myDataGridView1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 'r') { DataGridView dgv = sender as DataGridView; DataGridViewCell cell = dgv.CurrentCell; if (cell.IsInEditMode) { //限制单元格只能输入test if (cell.EditedFormattedValue != null && cell.EditedFormattedValue.ToString() != "test") { MessageBox.Show("输入内容不合格"); } else { dgv.CurrentCell = dgv[cell.ColumnIndex, cell.RowIndex + 1]; } } } }
当然为了保证用户直接将鼠标移动到新的单元格也能做数据的核对,我们还需要添加对这个控件的CellEndEdit事件的处理:
private void myDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridView dgv = sender as DataGridView; DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex]; { //限制单元格只能输入test if (cell.EditedFormattedValue != null && cell.EditedFormattedValue.ToString() != "test") { MessageBox.Show("输入内容不合格"); dgv.CurrentCell = cell ; } else { dgv.CurrentCell = dgv[cell.ColumnIndex, cell.RowIndex + 1]; } } }
这样无论是用户按的Enter键还是直接移动鼠标就都会做相应的验证了。
为了使得代码便于维护最好将判断校验代码单独写到一个方法,供这个两个事件处理调用。
转载自:http://hi.baidu.com/topkaze/item/40e83c0c46dc7519acdc701f
相关文章推荐
- DataGridView处于编辑状态的时候不响应Keyup事件
- DataGridView在Cell编辑状态响应回车键下的KeyPress/KeyDown/KeyUp事件
- 无线端不响应键盘事件(keydown,keypress,keyup)
- DataGridView单元格处于编辑状态触发KeyDown等事件
- 部分无线终端不响应键盘事件(keydown,keypress,keyup)的解决办法
- keyup 、keypress、keydown事件 onblur和onfocus事件
- KeyDown、KeyPress和KeyUp事件的区别与联系
- 键盘事件keypress 和 keydown、keyup 的用法与区别
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
- DataGridView中的Cell无法触发KeyPress事件,解决方法
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
- keyDown keyPress keyUp 事件的区别
- KeyDown、KeyPress和KeyUp事件的区别与联系
- 键盘事件keydown、keypress、keyup随笔整理总结
- Javascript跨浏览器处理键盘事件keydown,keypress,keyup
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
- 实现按钮的左键单击和右键单击事件 编辑框的回车键和ctrl+enter组合键消息响应
- 键盘事件keydown、keypress、keyup随笔整理总结
- keyDown keyPress keyUp 事件的区别
- 解决 jquery在iOS系统自带键盘无法触发keyup,keydown,keypress事件