I have a datagridview that populated by a datatable.All the three columns in it are set to read only so it actually acts like a popup with list.First column is filled with product code,Second column is filled with Sku Name and third one is filled with Mrp.And the Datagridview selection property is set to FullRowSelect.I use the keypress event to handle when user presses enter key.but the problem here is currentrow moves to next row ,instead of staying where user entered the enter button!!

So i handled the problem like this

private void dataGridView2_KeyPress(object sender, KeyPressEventArgs e)
{

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

but the problem when i select the last row,it selects the second last row Please help me out in this..

I already considered Cheating the DataGridView - CodeProject - The Code Project

有帮助吗?

解决方案

Try this

void dataGridView1_KeyDown(object.sender, KeyEventArgs e)
{
if (e.KeyCode == 13) 'Enter Key
{
this.dataGridView1.CurrentRow.Selected = true;
e.Handled = true;
}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top