c# - DataGridView,如何捕获单元格的KeyPress事件C#

标签 c# datagridview

我想在 datagridview c# 中对一个单元格进行处理,当我按下一个单元格时,这个特性会打开一个表单。

在 C# 中没有允许我直接添加处理的事件(按键)

在网上搜索后,我找到了以下解决方案

 private void dGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress +=
        new KeyPressEventHandler(Control_KeyPress);
    }


           private void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.A.ToString()) && Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.Z.ToString())) || (Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.D0.ToString()) && Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.D9.ToString()) || (Strings.Asc(e.KeyChar) >= 97 && Strings.Asc(e.KeyChar) > 122)))
            {
                ------
            }
      }

但它不起作用。 在调试中执行了事件 dGridView_EditingControlShowing 的代码,但是 Control_KeyPress 函数的代码没有运行

有什么想法吗

最佳答案

您应该将Form KeyPreview 属性设置为true。 你应该处理主窗体上的按键事件。 那是因为 Control.KeyPress 事件

Occurs when a key is pressed while the control has focus. (msdn)

public Form()
{
    InitializeComponent();
    this.KeyPreview = true;
    this.KeyPress += new KeyPressEventHandler(Control_KeyPress);
}

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    //your code
}

关于c# - DataGridView,如何捕获单元格的KeyPress事件C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836220/

相关文章:

c# - 使用 XmlWriter 附加到 XML 文件

c# - 在 gridview (C#) 中获取 SQLite 数据不起作用

mysql - 如何将datagridview表中的值与自定义结合起来

c# - DataGridView 的行为就好像它在 TableLayoutPanel 中具有最小尺寸一样

c# - 如何使用 SelectedRows 从数据 GridView 中获取选定的行数据?

c# - 更改数据集中记录的顺序

c# - 仅使用c# winform,不使用asp.netc#输入表单后如何检查用户是否登录

c# - 如何在 C# 字符串格式函数 Razor 中添加多个空格?

c# - 基于现有表创建 ASP.net 登录系统?

c# - 异步 GET 请求如何工作?