【发布时间】:2013-07-02 05:18:35
【问题描述】:
我们正在尝试在datagridviewcell 编辑模式下在enter key press 上执行代码块。但是我们无法在editing mode 中找到datagridviewcell 上的回车键。
【问题讨论】:
标签: c# windows vb.net winforms
我们正在尝试在datagridviewcell 编辑模式下在enter key press 上执行代码块。但是我们无法在editing mode 中找到datagridviewcell 上的回车键。
【问题讨论】:
标签: c# windows vb.net winforms
KeyDown 不适用于处于编辑模式的单元格,继承 DataGridView 并像这样覆盖 ProcessDialogKey。
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
// Your code here
return true;
}
return base.ProcessDialogKey(keyData);
}
【讨论】:
CurrentCell 属性来确定哪个单元格处于编辑模式以相应地执行您的代码。
您必须使用dataGridView1_KeyDown 事件,如下所示:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
//block of code
}
}
【讨论】:
如果您想检查单击了哪个单元格,请执行以下操作。这是在 VB.net 中。扩展了 Gary 已经向您提出的建议。
公共类 Custom_DataGridView 继承 System.Windows.Forms.DataGridView
公共事件DataGridView_CustomEnterKeyPressed(ByVal keyData As Keys, ByVal CurrentCell As DataGridViewCell)
Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean 如果 keyData = Keys.Enter 则 RaiseEvent DataGridView_CustomEnterKeyPressed(keyData, CType(Me, DataGridView).CurrentCell) '返回真'如果你不想移动到下一个单元格,打开这个,如果你打开这个,光标将不会移动到列中的下一个单元格。 万一 返回 MyBase.ProcessDialogKey(keyData) 结束函数
结束类
使用此 Custom_DataGridView 而不是开箱即用的 DataGridView 控件,然后您必须在添加此自定义控件的表单中处理 DataGridView_CustomEnterKeyPressed 事件,如下所示。
Private Sub DataGridNameYouHaveUsed_DataGridView_CustomEnterKeyPressed(ByVal keyData As System.Windows.Forms.Keys, ByVal CurrentCell As DataGridViewCell) 处理 DataGridNameYouHaveUsed.DataGridView_CustomEnterKeyPressed MsgBox("在 DataGridView_CustomEnterKeyPressed 的单元格事件处理程序中(: " + CurrentCell.ColumnIndex.ToString + "," + CurrentCell.RowIndex.ToString + ")") 结束子
您可能已经解决了您的问题,只是发布,以便如果其他人(如我)正在寻找解决方案,这可能对他们有用。
【讨论】: