2014-09-28 23 views
0

我嘗試了幾種組合,但沒有任何成功。當按下按鈕時,焦點在黃色(1)上,我想當按下輸入鍵時,焦點會轉到單元格(2),然後是(3),依此類推。使用回車鍵瀏覽datagridview單元格

enter image description here

如果沒有電池的編輯,只用回車鍵,一切都很好,此代碼:

Private Sub DGVBU1_KeyDown(sender As Object, e As KeyEventArgs) Handles DGVBU1.KeyDown 
     If e.KeyCode = Keys.Enter Then 
      Dim CLIndex As Integer = DGVBU1.CurrentCell.ColumnIndex 
      Dim RWIndex As Integer = DGVBU1.CurrentCell.RowIndex 
      If CLIndex = 2 Then 
       DGVBU1.CurrentCell = DGVBU1.Rows(RWIndex - 1).Cells(CLIndex + 1) 
      ElseIf CLIndex = 3 Then 
       DGVBU1.CurrentCell = DGVBU1.Rows(RWIndex).Cells(CLIndex - 1) 
      End If 
     End If 
    End Sub 

但當Enter鍵單元格編輯後,按下它下降到行+ 1 。

我也試過這樣:

Private Sub DGVBU1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGVBU1.CellEndEdit 
    Dim CLIndex As Integer = DGVBU1.CurrentCell.ColumnIndex 
    Dim RWIndex As Integer = DGVBU1.CurrentCell.RowIndex 
    If CLIndex = 2 Then 
     DGVBU1.CurrentCell = DGVBU1.Rows(RWIndex).Cells(CLIndex - 1) 
     DGVBU1.CurrentCell = DGVBU1.Rows(8).Cells(CLIndex + 1) 
    ElseIf CLIndex = 3 Then 
     DGVBU1.CurrentCell = DGVBU1.Rows(RWIndex).Cells(CLIndex - 1) 
    End If 
End Sub 

...但隨後從輸入鍵發送焦點(1)直接連接到電池(4)。

是否有可能使這項工作?

感謝您的任何幫助。

回答

0

如果DataGridView可能會攔截keydown事件中的Enter鍵。如果是這樣的問題,一個解決方案是導出控制類並覆蓋ProcessCmdKey:

受保護的覆蓋功能ProcessCmdKey(MSG的ByRef作爲報文,BYVAL KEYDATA作爲鍵)爲布爾

如果KEYDATA = Keys.Enter然後 的onkeydown(新KeyEventArgs(KEYDATA)) ProcessCmdKey =真 否則 ProcessCmdKey = MyBase.ProcessCmdKey(味精,KEYDATA) 結束如果

端功能

+0

但也禁用任何使用Enter鍵的代碼,以及通過帶有Enter鍵的DGV導航。 – Jovica 2014-09-28 21:15:58

+0

的確如此。您需要單獨處理該代碼。 – xpda 2014-09-28 22:04:14

+0

對不起,但我不知道該怎麼做? – Jovica 2014-09-29 05:14:42