Can I make DataGridView.EndEdit trigger the CellValidating event?

后端 未结 5 582
醉话见心
醉话见心 2021-01-12 02:14

I\'m using a DataGridView in my WinForms application. My main objective is to make the Enter key not move to the next row in the grid. I still want the enter key to validate

5条回答
  •  生来不讨喜
    2021-01-12 02:37

    thanks for the solution. my version is a slight different from yours, because when i move to the other cell, and my code returns e.cancel=false in the cell validating event, an error will be generated, says that: "operation did not succeed, because the program cannot commit or quit a cell value change". so i put try catch to overcome this problem.

    this is my code:

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    
        Dim key As Keys = (keyData And Keys.KeyCode)
    
        If key = Keys.Enter Then
            If MyBase.CurrentCell.ColumnIndex = 1 Then
                Dim iRow As Integer = MyBase.CurrentCell.RowIndex
    
                MyBase.EndEdit()
                Try
                    MyBase.CurrentCell = Nothing
                    MyBase.CurrentCell = MyBase.Rows(iRow).Cells(1)
                    frmFilter.cmdOk_Click(Me, New EventArgs)
                Catch ex As Exception
                End Try
    
                Return True
            End If
        End If
    
        Return MyBase.ProcessDialogKey(keyData)
    End Function
    

提交回复
热议问题