2013-10-25 78 views
2

這裏是我的單元結束編輯和選擇改變事件代碼:如何捕捉datagridview的細胞最終編輯箭頭鍵在VB.net

Private Sub dvJOBranch_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvJOBranch.CellEndEdit 

    'get the current column and row index 
    curCol = e.ColumnIndex 
    curRow = e.RowIndex 

    'if the cell is blank, set it to zero 
    If IsDBNull(dvJOBranch.Rows(curRow).Cells.Item(curCol).Value) Then 
     dvJOBranch.Rows(curRow).Cells.Item(curCol).Value = 0 

     'convert it to integer 
    Else 
     dvJOBranch.Rows(curRow).Cells.Item(curCol).Value = _ 
      Convert.ToInt32(dvJOBranch.Rows(curRow).Cells.Item(curCol).Value.ToString()) 
    End If 



    'if the user do mouseclick in datagridview 
    isMouseClick = dvJOBranch.Capture.ToString() 

    'if the user does not click any cell from the datagridview 
    If isMouseClick = False Then 

     isEdited = True 
     iColumnindex = e.ColumnIndex 
     irowindex = e.RowIndex 

     If dvJOBranch.CurrentRow.Index = dvJOBranch.RowCount - 1 Then 
      If dvJOBranch.CurrentCell.ColumnIndex < dvJOBranch.ColumnCount - 1 Then 
       dvJOBranch.CurrentCell = dvJOBranch.Item(iColumnindex + 1, irowindex) 
      End If 
      isEdited = False 
     End If 

    End If 



End Sub 

Private Sub dvJOBranch_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvJOBranch.SelectionChanged 

    'if the user does not click any cell from the datagridview 
    If isMouseClick = False Then 

     If isEdited Then 
      If dvJOBranch.CurrentCell.ColumnIndex < dvJOBranch.ColumnCount - 1 Then 
       dvJOBranch.CurrentCell = dvJOBranch.Item(iColumnindex + 1, irowindex) 
      Else 
       dvJOBranch.CurrentCell = dvJOBranch.Item(2, irowindex + 1) 
      End If 
      isEdited = False 
     End If 


    End If 

    'set it to false 
    isMouseClick = False 

End Sub 

這段代碼的功能是當前單元格移到在使用ENTER鍵進行編輯後,在我的代碼中,我還捕獲鼠標單擊,如果用戶單擊任何單元,因爲它有錯誤如果我沒有捕獲鼠標點擊,我現在要做的是捕獲箭頭鍵我正在編輯。因爲在編輯一個單元格後,例如當我按下箭頭鍵UP時,它會像我的ENTER鍵的功能一樣向右移動,而不是向上移動。

任何幫助和指導可以理解,謝謝

回答

2

您可以使用的datagridview這樣的KeyUp事件:

Private Sub dvJOBranch(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dvJOBranch.KeyUp 
    If e.KeyCode = Keys.Up Then 
     ' Do your code here 
    End If 
End Sub 

如果你想要回車鍵在那裏進行處理你也可以使用一個選擇案例:

Private Sub dvJOBranch(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dvJOBranch.KeyUp 
    Select Case e.KeyCode 
     Case Keys.Enter 
      ' Code for enter 
     Case Keys.Up 
      ' Code for up arrow 
      'Etc 
    End Select 
End Sub