我想在 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/