2013-07-02 82 views
0

执行对输入按键代码块我们正试图在datagridviewcell编辑在enter key press执行的代码块mode.But我们无法找到editing modedatagridviewcell输入按键。在DataGridViewCell的

回答

0

你将不得不使用dataGridView1_KeyDown事件如下:

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

在哪里执行代码 – user2541238

+0

里面如果条件 –

+0

它也可以在datagridviewcell编辑模式下工作。 – user2541238

1

的KeyDown不会在编辑模式下,子类的DataGridView单元工作,覆盖ProcessDialogKey这样。

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

先生我们要做的是,在按下输入单元格中的键1执行代码块1,然后在单元格2中按回车键执行代码块2等等。什么block1的代码是它会显示一个包含listview的模式框。 – user2541238

+0

我已经向你展示了如何拦截回车键,添加你的代码来显示我指出的对话框。 – Gary

+0

@ user2541238您必须引用'CurrentCell'属性来确定哪个单元处于编辑模式以相应地执行您的代码。 –

0

如果要检查正在单击哪个单元格,请执行下列操作。这是在VB.net中。扩展Gary已经建议你的东西。

公共类Custom_DataGridView 继承System.Windows.Forms.DataGridView

公共事件DataGridView_CustomEnterKeyPressed(BYVAL KEYDATA作为键,BYVAL CurrentCell作为的DataGridViewCell)

受保护的覆盖功能ProcessDialogKey(BYVAL KEYDATA作为键)为布尔 如果keyData = Keys.Enter Then RaiseEvent DataGridView_CustomEnterKeyPressed(keyData,CType(Me,DataGridView).CurrentCell) '返回true'如果您不想移动到下一个单元格,请打开此项,如果您打开此选项,光标将不会转移到n列中的外部单元格。 结束如果 返回MyBase.ProcessDialogKey(KEYDATA) 端功能

末级

使用此Custom_DataGridView而不是开箱DataGridView控件,然后你必须处理DataGridView_CustomEnterKeyPressed事件在您的形式显示在下面您在其中添加了此自定义控件。

私人小组DataGridNameYouHaveUsed_DataGridView_CustomEnterKeyPressed(BYVAL KEYDATA作为System.Windows.Forms.Keys,BYVAL CurrentCell作为DataGridViewCell的)把手DataGridNameYouHaveUsed.DataGridView_CustomEnterKeyPressed MSGBOX( “以DataGridView_CustomEnterKeyPressed事件处理程序单元(:” + CurrentCell.ColumnIndex.ToString +”, “+ CurrentCell.RowIndex.ToString +‘)’) 结束小组

您可能已经解决了您的问题,只是张贴这样,如果别人(像我)正在寻找一个解决方案,这可能是对他们有用。