【问题标题】:DataGridView "Enter" key event handlingDataGridView“回车”键事件处理
【发布时间】:2014-03-19 08:42:29
【问题描述】:

我有一个用 DataTable 填充的 DataGridView,有 10 列。当我单击 Enter 键时从一行移动到另一行时,我有一个场景,然后我需要选择该行并且需要具有该行值。

但是在这里,当我选择第 n 行时,它会自动移动到 n+1 行。

请帮帮我...

在页面加载事件中:

SqlConnection con = 
    new SqlConnection("Data Source=.;Initial Catalog=MHS;User ID=mhs_mt;Password=@mhsinc");

DataSet ds = new System.Data.DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from MT_INVENTORY_COUNT", con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

那么,

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == (Char)Keys.Enter)
     {
           int i = dataGridView1.CurrentRow.Index;
           MessageBox.Show(i.ToString());
     }     
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int i = dataGridView1.CurrentRow.Index;
    MessageBox.Show(i.ToString());
}

【问题讨论】:

  • 你想按回车键然后选择移动到下一行啊?
  • 不,我想要当前行本身

标签: c# .net winforms c#-4.0 datagridview


【解决方案1】:

以下解决方案更简单,也有效:

  1. 在 .Designer.cs 文件中:

    this.dataGridView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridView1_KeyDown);

  2. 在文件后面的代码中:

     private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
     {
         if (e.KeyData == Keys.Enter)
         {
             // Handle event
             e.Handled = true;
         }
     }
    

【讨论】:

  • 这应该是公认的答案
【解决方案2】:

使用以下代码添加一个类并修改您的 GridView 设计器页面,即,

this.dataGridView1 = New System.Windows.Forms.DataGridView();

到

this.dataGridView1 = new GridSample_WinForms.customDataGridView();

类文件是:

class customDataGridView : DataGridView
{
  protected override bool ProcessDialogKey(Keys keyData)
  {
     if (keyData == Keys.Enter)
     {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
        this.CurrentCell = this[col, row];
        return true;
     }
     return base.ProcessDialogKey(keyData);
  }

  protected override void OnKeyDown(KeyEventArgs e)
  {
     if (e.KeyData == Keys.Enter)
     {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
        this.CurrentCell = this[col, row];
        e.Handled = true;
     }
     base.OnKeyDown(e);
  }
}

【讨论】:

  • 谢谢,我正在找这个。如果我在单元格中,ProcessDialogKey 是如何捕获 KeyDown 的方式
【解决方案3】:

这是 DataGridView 的默认行为,也是第三方供应商在其他数据网格中的标准行为。

会发生什么:

  1. 用户按回车键
  2. DataGridView 接收 KeyPress 事件并执行各种操作(如结束编辑等),然后将单元格下移一行。
  3. 然后,DataGridView 会检查是否有任何事件处理程序被您连接并触发这些事件处理程序。

所以当按下回车键时,当前单元格已经改变了。

如果您想在 DataGridView 更改行之前获取用户所在的行,可以使用以下方法。这应该适合您现有的代码(显然您需要为其添加事件处理程序):

void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        int i = dataGridView1.CurrentRow.Index;
        MessageBox.Show(i.ToString());
    }     
}

我希望这有助于为您指明正确的方向。不确定您希望在这里做什么,但希望这能解释您所看到的情况。

【讨论】:

  • 我错了,正如我所料,CurrentRow 索引自动增加 1 但与 GridView 的最后一行不同。有什么帮助吗?
  • 如果您连接 PreviewKeyDown 事件,它将在 DataGridView 将单元​​格向下移动一行之前触发。例如,我根据我的建议运行了您的代码,并得到了两个消息框。一个说“2”——因为它在第 3 行,下一个——来自 KeyPress 事件说“3”——因为它在调用该处理程序之前向下移动到下一行。这不是你所经历的吗?
  • 我应该提到,如果你正在编辑一个单元格,当按下 Enter 键时 PreviewKeyDown 事件不会触发,但 KeyPress 事件也不会触发。这是因为用于编辑 DataGridView 中数据的文本框处理键盘输入。您也可以为其附加一个处理程序,但这需要更多的工作。如果您希望我向您展示如何操作,请告诉我。
  • 在我的场景中,当我按下“Enter”键时,应该选择该行并需要捕获这些值。
  • 不确定您要完成什么。这篇帮助文章中有什么内容吗:link?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
相关资源
最近更新 更多