2010-03-26 46 views
1

当我编辑一个单元格并按下回车键时会自动选择下一行,我想留在当前行...除了EndEdit之外我什么也没有发生。如何在DataGridView上按下“enter”键时更改发生了什么?

我有这样的:

private void dtgProductos_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
     { 
      dtgProductos[e.ColumnIndex, e.RowIndex].Selected = true; //this line is not working 
      var index = dtgProductos.SelectedRows[0].Cells.IndexOf(dtgProductos.SelectedRows[0].Cells[e.ColumnIndex]); 
      switch (index) 
      { 
       case 2: 
        { 
         dtgProductos.SelectedRows[0].Cells[4].Selected = true; 
         dtgProductos.BeginEdit(true); 
        } 
        break; 
       case 4: 
        { 
         dtgProductos.SelectedRows[0].Cells[5].Selected = true; 
         dtgProductos.BeginEdit(true); 
        } 
        break; 
       case 5: 
        { 
         btnAddProduct.Focus(); 
        } 
        break; 
       default: 
        break; 
      } 
     } 

所以当我编辑的行不是最后一个我得到这个错误:

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function. 

回答

2

我认为你需要重写ProcessEnterKey不提前关注下一行。

也许this thread将有所帮助。

+0

我无法找到重写哪种方法,你能帮我解决吗? – Luiscencio 2010-03-26 23:26:27

+0

现在我知道了......我把重写方法留空了,但我仍然得到了错误=( – Luiscencio 2010-03-26 23:43:42

+1

添加了一个链接,我发现使用google搜索短语“改变默认行为的输入密钥datagridview”(因为推进行是默认行为) 。 – John 2010-03-26 23:48:09

相关问题