DGV Validate Cell and Navigate to Next Row in EditMode

Aashish Agarwal 0 Reputation points
2024-08-19T17:10:12.5866667+00:00

Hi

I have a DGV With 12 column, the columns 6-12 are only for information hence set to readonly.. What i was looking for is when the user press Enter key in columns 1 to 5, if the cell data Validates then next cell in same row should be selected, after col5 the Enter key should set the focus to next row col1.

After Googling i was able to partially achieve the requirement,

The problem is when the dgv is in edit mode and enter key is pressed in col5, then col6 of same row is being selected.

I have a hint of adding a handle to EditingControl and handling the KeyDown Event, but KeyDown event of EditingControl is being fired first after then cellvalidating of the DGV, hence cant set the focus to next cell before cell validating

kindly help to achieve this,

Regads

TIA

Public Class mydgv
    Inherits DataGridView
    Dim ResetCol As Integer = 4


    Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
        Dim key As Keys = keyData And Keys.KeyCode

        If key = Keys.Enter Then
            Return Me.ProcessTabKey(keyData)
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function


    Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
        If e.KeyCode = Keys.Enter Then
            Dim currPos As New Point(0, 0)
            Dim NextRowId As Integer = -1

            If MyBase.CurrentCell IsNot Nothing Then currPos = New Point(MyBase.CurrentCell.RowIndex, MyBase.CurrentCell.ColumnIndex)
            NextRowId = currPos.X

            If currPos.Y >= Me.ResetCol Then

                If NextRowId < Me.RowCount - 1 Then
                    NextRowId += 1
                End If
                MyBase.CurrentCell = MyBase.Rows(NextRowId).Cells(0)
                Return True
            Else
                Return Me.ProcessTabKey(e.KeyData)
            End If

        End If
        Return MyBase.ProcessDataGridViewKey(e)
    End Function

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,823 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,720 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,091 Reputation points Microsoft Vendor
    2024-08-20T02:35:21.22+00:00

    Hi,

    To achieve the desired behavior, you can override the ProcessDataGridViewKey and ProcessDialogKey methods as you did, but with additional logic to handle the transition from column 5 to column 1 of the next row. You should also consider the CellValidating event to ensure that the validation occurs before moving the focus.

    Public Class mydgv
        Inherits DataGridView
    
        Dim ResetCol As Integer = 4 ' Index of the last editable column
    
        Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
            Dim key As Keys = keyData And Keys.KeyCode
    
            If key = Keys.Enter Then
                Return True
            End If
    
            Return MyBase.ProcessDialogKey(keyData)
        End Function
    
        Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
            If e.KeyCode = Keys.Enter Then
                If Me.CurrentCell IsNot Nothing AndAlso Me.IsCurrentCellInEditMode Then
                    If Not Me.EndEdit() Then
                        Return True ' Cancel if validation fails
                    End If
                End If
    
                Dim currPos As New Point(Me.CurrentCell.RowIndex, Me.CurrentCell.ColumnIndex)
    
                If currPos.Y = Me.ResetCol Then
                    If currPos.X < Me.RowCount - 1 Then
                        Me.CurrentCell = Me.Rows(currPos.X + 1).Cells(0)
                    End If
                Else
                    Me.CurrentCell = Me.Rows(currPos.X).Cells(currPos.Y + 1)
                End If
    
                Return True ' Suppress the default handling
            End If
    
            Return MyBase.ProcessDataGridViewKey(e)
        End Function
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.