DataGridView的keydown事件不工作在C# [英] DataGridView keydown event not working in C#

查看:1537
本文介绍了DataGridView的keydown事件不工作在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGridView的keydown事件是不是当我编辑一个单元格中的文字工作。

我指定快捷键 Alt键+ S 以保存数据,它的工作原理,当小区不是在编辑模式,但如果是在低于code编辑模式是行不通的。

 私人无效dataGridView1_KeyDown(对象发件人,KeyEventArgs E)
 {
    如果(e.KeyData ==(Keys.Alt | Keys.S))
    {
         //保存数据
    }
 }
 

解决方案

每当一个细胞在编辑模式下,其托管控制接收的KeyDown 事件,而不是父的DataGridView 包含它。这就是为什么你的键盘快捷键工作时的小区是不是在编辑模式下(即使它被选中),因为你的的DataGridView 控制自己收到的KeyDown 事件。但是,当你在编辑模式下,包含的单元格中的编辑控件接收事件,并没有任何反应,因为它不具备自定义处理程序连接到它。

我已经花了太多的时间调整的标准的DataGridView 控制处理编辑提交我想它的方式,我发现的最简单的方法解决这种现象是通过继承现有的的DataGridView 控制和覆盖它的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx"><$c$c>ProcessCmdKey功能 。无论自定义,你摆在这里将运行时的一个关键是pssed上的的DataGridView 的顶部$ P $ code,不管它是不是在编辑模式

例如,你可以做这样的事情:

 类MyDataGridView:System.Windows.Forms.DataGridView
{
    保护覆盖布尔ProcessCmdKey(REF System.Windows.Forms.Message味精,System.Windows.Forms.Keys KEYDATA)
    {
        如果((KEYDATA ==(Keys.Alt | Keys.S)))
        {
            //保存数据
        }

        返回base.ProcessCmdKey(味精,KEYDATA);
    }
}
 

另请参阅相关的,虽然有点老,文章:如何捕获的控制按键使用Visual C#

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
    }
 }

解决方案

Whenever a cell is in edit mode, its hosted control is receiving the KeyDown event instead of the parent DataGridView 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 your DataGridView control itself receives the KeyDown 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 existing DataGridView control and overriding its ProcessCmdKey function. Whatever custom code that you put in here will run whenever a key is pressed on top of the DataGridView, regardless of whether or not it is in edit mode.

For example, you could do something like this:

class MyDataGridView : System.Windows.Forms.DataGridView
{
    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        if ((keyData == (Keys.Alt | Keys.S)))
        {
            //Save data
        }

        return base.ProcessCmdKey(msg, keyData);
    }
}

Also see related, though somewhat older, article: How to trap keystrokes in controls by using Visual C#

这篇关于DataGridView的keydown事件不工作在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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