按ENTER键选择datagridview中的行

时间:2014-01-22 16:16:33

标签: datagridview key enter

我有一个由datatable填充的datagridview。其中的三列被设置为只读,所以它实际上就像一个带有list的弹出窗口。第一列用产品代码填充,第二列用Sku Name填充第三个用Mrp填充。并且Datagridview选择属性设置为FullRowSelect。我使用keypress事件来处理用户按下enter键的时候。但是这里的问题是currentrow移动到下一行,而不是停留在用户输入enter按钮的位置!!

所以我处理了这样的问题

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

1 个答案:

答案 0 :(得分:2)

试试这个

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