2010-03-26 41 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

相關問題