简体   繁体   English

在datagridviewcell中的Enter键上执行代码块

[英]execute block of code on enter key press in datagridviewcell

我们正在尝试在datagridviewcell editing modeenter key press执行代码块。但是在editing mode我们无法在datagridviewcell上找到Enter键。

KeyDown will not work for a cell in editing mode, subclass DataGridView and override ProcessDialogKey like this. KeyDown在编辑模式下不适用于单元格,它是DataGridView的子类,并像这样重写ProcessDialogKey。

protected override bool ProcessDialogKey(Keys keyData)
{
    if (keyData == Keys.Enter)
    {
        // Your code here
        return true;
    }
    return base.ProcessDialogKey(keyData);
}

You will have to use dataGridView1_KeyDown event as follows: 您将必须使用dataGridView1_KeyDown事件,如下所示:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {                
                e.SuppressKeyPress=true;
                //block of code
            }
         }

Do following if you want to check which cell is being clicked. 如果要检查单击哪个单元格,请执行以下操作。 This is in VB.net. 这是在VB.net中。 Extended what Gary has already suggested you. 扩展了Gary已经建议您的内容。

Public Class Custom_DataGridView Inherits System.Windows.Forms.DataGridView 公共类Custom_DataGridView继承System.Windows.Forms.DataGridView

Public Event DataGridView_CustomEnterKeyPressed(ByVal keyData As Keys, ByVal CurrentCell As DataGridViewCell) 公共事件DataGridView_CustomEnterKeyPressed(ByVal keyData作为键,ByVal CurrentCell作为DataGridViewCell)

Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean If keyData = Keys.Enter Then RaiseEvent DataGridView_CustomEnterKeyPressed(keyData, CType(Me, DataGridView).CurrentCell) 'Return true 'Open this if you don't want to move to next cell, if you open this the cursor will not move to next cell in the column. 受保护的重写函数ProcessDialogKey(ByVal keyData As Keys)As Boolean如果keyData = Keys.Enter,则RaiseEvent DataGridView_CustomEnterKeyPressed(keyData,CType(Me,DataGridView).CurrentCell)'返回true'如果不想移动到下一个单元格,请打开此选项,如果打开它,光标将不会移动到列中的下一个单元格。 End If Return MyBase.ProcessDialogKey(keyData) End Function 如果返回,则结束MyBase.ProcessDialogKey(keyData)结束函数

End Class 末级

Use this Custom_DataGridView instead of out of the box DataGridView Control and then you have to handle the DataGridView_CustomEnterKeyPressed event as shown below in your form in which you have added this custom control. 使用此Custom_DataGridView代替开箱即用的DataGridView控件,然后必须处理DataGridView_CustomEnterKeyPressed事件,如下所示,在您添加此自定义控件的表单中。

Private Sub DataGridNameYouHaveUsed_DataGridView_CustomEnterKeyPressed(ByVal keyData As System.Windows.Forms.Keys, ByVal CurrentCell As DataGridViewCell) Handles DataGridNameYouHaveUsed.DataGridView_CustomEnterKeyPressed MsgBox("In DataGridView_CustomEnterKeyPressed Event Handler for cell( : " + CurrentCell.ColumnIndex.ToString + "," + CurrentCell.RowIndex.ToString + ")") End Sub 私有子DataGridNameYouHaveUsed_DataGridView_CustomEnterKeyPressed(ByVal keyData作为System.Windows.Forms.Keys,ByVal CurrentCell作为DataGridViewCell)处理DataGridNameYouHaveUsed.DataGridView_CustomEnterKeyPressed MsgBox(“在DataGridView_CustomEnterKeyPressed中为单元格处理程序:” + CurrentCell.Index。 .ToString +“)”)结束子

You might have already solved your problem, just posting so that if somebody else (like me) is searching for a solution this might be useful to them. 您可能已经解决了您的问题,只需发布​​,以便其他人(例如我)正在寻找解决方案时,这可能对他们有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM