Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

What I have tried:

VB
<pre>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
Posted
Comments
PIEBALDconsult 19-Aug-24 15:59pm    
Don't use a DataGridView.

1 solution

Use a CurrentCell.EndEdit() in key event (keyPressed, KeyDown,...)

Reference to:
DataGridView.EndEdit Method (System.Windows.Forms) | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900