DataGridView keydown 事件在 C# 中不起作用
当我在单元格内编辑文本时,DataGridView keydown 事件不起作用。
我正在指定快捷键 Alt+S 来保存数据,当单元格不处于编辑模式时它可以工作,但如果它处于编辑模式,下面的代码将不起作用
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Alt | Keys.S))
{
//save data
}
}
DataGridView keydown event is not working when I am editing text inside a cell.
I am assigning shortcut Alt+S to save the data, it works when cell is not in edit mode, but if it is in edit mode below code is not working
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Alt | Keys.S))
{
//save data
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只要单元格处于编辑模式,其托管控件就会接收
KeyDown
事件,而不是包含该单元格的父DataGridView
事件。 这就是键盘的原因只要单元格未处于编辑模式(即使已选择),快捷方式就会起作用,因为您的 DataGridView 控件本身会接收 KeyDown 事件。但是,当您处于编辑模式时,单元格包含的编辑控件正在接收事件,但不会发生任何事情,因为它没有附加自定义处理程序例程。我花了太多时间调整标准
DataGridView
控件以按照我想要的方式处理编辑提交,并且我发现解决这种现象的最简单方法是子类化现有的DataGridView
控件并覆盖其ProcessCmdKey
函数。无论您在此处输入什么自定义代码,只要按下DataGridView
顶部的某个键,无论它是否处于编辑模式,都将运行。例如,您可以执行以下操作:
另请参阅相关但有些旧的文章:如何捕获击键使用 Visual C# 进行控制
Whenever a cell is in edit mode, its hosted control is receiving the
KeyDown
event instead of the parentDataGridView
that contains it. That's why your keyboard shortcut is working whenever a cell is not in edit mode (even if it is selected), because yourDataGridView
control itself receives theKeyDown
event. However, when you are in edit mode, the edit control contained by the cell is receiving the event, and nothing happens because it doesn't have your custom handler routine attached to it.I have spent way too much time tweaking the standard
DataGridView
control to handle edit commits the way I want it to, and I found that the easiest way to get around this phenomenon is by subclassing the existingDataGridView
control and overriding itsProcessCmdKey
function. Whatever custom code that you put in here will run whenever a key is pressed on top of theDataGridView
, regardless of whether or not it is in edit mode.For example, you could do something like this:
Also see related, though somewhat older, article: How to trap keystrokes in controls by using Visual C#
另一种方法是使用 EditingControlShowing 事件将事件处理重定向到自定义事件处理程序,如下所示:
Another way of doing it is by using the EditingControlShowing event to redirect the event handling to a custom event handler as below:
确实,EditingControlShowing 可以提供帮助,但如果您想捕获 Enter 键,则不然。在这种情况下,应该使用以下方法:
This is true that EditingControlShowing can help, but not if you wants to catch the Enter key. In that case, one should use the following method:
我刚刚尝试的一个更简单的方法如下:
KeyPreview
属性设置为true
。代码如下:
A simpler way I just tried out is as follows:
KeyPreview
property of the Form totrue
.Code as follows:
我用这个工作过
I worked with this
解决方案
类 MyDataGridView : System.Windows.Forms.DataGridView {
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) {
if ( keyData == Keys.Enter ) {
。
处理输入键
。
}
返回base.ProcessCmdKey(ref msg, keyData);
}
很好
对我来说效果
The solution
class MyDataGridView : System.Windows.Forms.DataGridView {
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) {
if ( keyData == Keys.Enter ) {
.
Process Enter Key
.
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Worked perfectly for me
使用
PreviewKeyDown
事件use
PreviewKeyDown
event