【问题标题】:Selecting a row in datagridview by pressing ENTER key通过按 ENTER 键在 datagridview 中选择一行
【发布时间】:2014-01-22 16:16:32
【问题描述】:

我有一个由数据表填充的数据网格视图。其中的所有三列都设置为只读,因此它实际上就像一个带有列表的弹出窗口。第一列填充产品代码,第二列填充 Sku 名称和第三个填充了Mrp。并且Datagridview选择属性设置为FullRowSelect。当用户按下回车键时,我使用keypress事件来处理。但这里的问题是当前行移动到下一行,而不是停留在用户输入回车按钮的位置!!

所以我就这样处理了这个问题

private void dataGridView2_KeyPress(object sender, KeyPressEventArgs e)
{

  if (e.KeyChar == 13)
  {
      dataGridView2.CurrentCell = dataGridView2[0, dataGridView2.CurrentCell.RowIndex -1];
      dataGridView2.CurrentRow.Selected = true;
  }
}

但是当我选择最后一行时出现问题,它选择倒数第二行 请帮我解决这个问题..

我已经考虑过Cheating the DataGridView - CodeProject - The Code Project

【问题讨论】:

标签: datagridview key enter


【解决方案1】:

试试这个

void dataGridView1_KeyDown(object.sender, KeyEventArgs e)
{
if (e.KeyCode == 13) 'Enter Key
{
this.dataGridView1.CurrentRow.Selected = true;
e.Handled = true;
}
}

【讨论】:

    猜你喜欢
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多