How to move only Current Row Column(4) and Column(5) using Enter Key in datagridview for VB.Net

dopen oinam 1 Reputation point
2021-02-22T19:21:10.91+00:00

I have facing some problem in datagridview current row movement. I want entry some number in Column(4) then press Enter key then next focus to Column(5) entry somthing then press Enter key add new row then focus. I was write some code. Someone know please correction or edit it.

Private Sub DGV1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DGV1.CellValidating
        Dim Err As Boolean
        For Each row As DataGridViewRow In DGV1.Rows
            If row.Cells("Qty").Value = String.Empty Then
                Err = True
                Exit For
            End If
        Next row
End Sub

 Private Sub DGV1_KeyDown(sender As Object, e As KeyEventArgs) Handles DGV1.KeyDown

 If e.KeyCode = Keys.Enter Then

            Dim vw As DataGridView = DirectCast(sender, DataGridView)
            'check for last row
            If vw.CurrentCell.RowIndex = vw.Rows.Count - 1 Then
                'if it's not the last cell, move to the next one
                If vw.CurrentCell.ColumnIndex <> vw.ColumnCount - 1 Then
                    vw.CurrentCell = vw.Rows(vw.CurrentCell.RowIndex).Cells(vw.CurrentCell.ColumnIndex + 1)
                Else
                    ' if last cell then either enter new raw or move to first cell.
                    'move to first column in new row
                    vw.Rows.Add(1)
                    vw.Refresh()
                    SendKeys.Send("{HOME}")
                End If
            Else
                'if it's the last cell, move to the next row, first cell
                If vw.CurrentCell.ColumnIndex = vw.ColumnCount - 1 Then
                    vw.CurrentCell = vw.Rows(vw.CurrentCell.RowIndex + 1).Cells(0)
                Else
                    'move to the next cell on the current row
                    vw.CurrentCell = vw.Rows(vw.CurrentCell.RowIndex).Cells(vw.CurrentCell.ColumnIndex + 1)
                End If
            End If
            'mark the keycode as handled
            e.Handled = True
        End If
End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,735 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,516 Reputation points Microsoft Vendor
    2021-02-23T07:47:42.87+00:00

    Hi @dopen oinam ,

    You can try to override ProcessCmdKey method to replace datagridview keydown event to move the cell focus correctly.

    Code:

     Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean  
            If keyData = Keys.Enter Then  
      
                'check for last row  
                If DataGridView1.CurrentCell.RowIndex = DataGridView1.Rows.Count - 1 Then  
                    'if it's not the last cell, move to the next one  
                    If DataGridView1.CurrentCell.ColumnIndex <> DataGridView1.ColumnCount - 1 Then  
      
                        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(DataGridView1.CurrentCell.ColumnIndex + 1)  
      
      
                    Else  
                            ' if last cell then either enter new raw or move to first cell.  
                            'move to first column in new row  
                            DataGridView1.Rows.Add(1)  
                        DataGridView1.Refresh()  
                        SendKeys.Send("{HOME}")  
                    End If  
                Else  
                    'if it's the last cell, move to the next row, first cell  
                    If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1 Then  
                        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(0)  
                    Else  
                        'move to the next cell on the current row  
                        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(DataGridView1.CurrentCell.ColumnIndex + 1)  
                    End If  
                End If  
      
      
            End If  
            Return MyBase.ProcessCmdKey(msg, keyData)  
      
        End Function  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            DataGridView1.ColumnCount = 5  
            DataGridView1.Columns(0).Name = "Product ID"  
            DataGridView1.Columns(1).Name = "Product Name"  
            DataGridView1.Columns(2).Name = "Product_Price"  
            DataGridView1.Columns(3).Name = "Product_Address"  
            DataGridView1.Columns(4).Name = "Product_Owner"  
      
            Dim row As String() = New String() {"1", "Product 1", "1000", "test1", "o1"}  
            DataGridView1.Rows.Add(row)  
            row = New String() {"2", "Product 2", "2000", "test2", "o2"}  
            DataGridView1.Rows.Add(row)  
            row = New String() {"3", "Product 3", "3000", "test3", "o3"}  
            DataGridView1.Rows.Add(row)  
            row = New String() {"4", "Product 4", "4000", "test4", "o4"}  
            DataGridView1.Rows.Add(row)  
        End Sub  
    

    Result:

    70980-2.gif


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.