How to make the "Enter" key acts like "Tab" key in Datagrid View ( VB.Net 2008 )?

Locked How to make the "Enter" key acts like "Tab" key in Datagrid View ( VB.Net 2008 )?

  • Tuesday, February 22, 2011 5:42 AM
     
      Has Code

    For example I have 5 columns in the datagrid and I'm currently in the first cell of the first row. When I press Enter key, I want the cursor to go to the next column of the same row but if it is the last column I want the cursor go the the first column of the next row.

     

    Actually, I already have the code

    Here is the Code:

    ___________________________

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

            If Me.SERVFILEDataGridView.CurrentCell.ColumnIndex <> Me.SERVFILEDataGridView.Columns.Count - 1 Then
                Dim nextindex As Integer = Me.SERVFILEDataGridView.CurrentCell.ColumnIndex + 1
                Dim method As New SetColumnIndex(AddressOf Mymethod)
                Me.SERVFILEDataGridView.BeginInvoke(method, nextindex)
             End If

    End Sub

    Delegate Sub SetColumnIndex(ByVal i As Integer)

    Private Sub Mymethod(ByVal columnIndex As Integer)
            Me.SERVFILEDataGridView.CurrentCell = Me.SERVFILEDataGridView.CurrentRow.Cells(columnIndex)
    End Sub

    ________________________

    With that code, when i click the first cell of the first row and hit enter, it will go to the second cell of the first row. But there is a problem

    If I will click the first cell of the first row and type something and then hit enter, the cursor should go the the second column but it is not happening. The cursor will go the the next column in the next row. It should be the next column of the same row.

    I came across this tutorial http://social.msdn.microsoft.com/Forums/en/vbpowerpacks/thread/18a63eb5-2541-40b7-9ab2-9cf468b0d4a4 (another approach) but I don't know how to do it because I am new to VB.net, overriding something like that. Please help me.

     

    Allan Posadas

All Replies

  • Tuesday, February 22, 2011 10:31 AM
     
     Answered

    Hi Allan,

    You may have meant going to next cell on the same row by highlighting next cell instead of going down horizontally. I know, this need is comes alive when your DataGridView's AllowUserToAddRows property is set to True , so:

    Use CellEndEdit event like this:

       Private Sub SERVFILEDataGridView_CellEndEdit _
    (ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles SERVFILEDataGridView.CellEndEdit
            If Me.SERVFILEDataGridView.CurrentCell.ColumnIndex <> _
    Me.SERVFILEDataGridView.ColumnCount - 1 Then
                SendKeys.Send("{TAB}")
                SendKeys.Send("{UP}")
            Else
                SendKeys.Send("{HOME}")
             End If
        End Sub

    SendKeys.Send("{UP}") is required because of having AllowUserToAddRows property as True to go to next cell on the same row, the highlighted cell will be on the right-bottom position, so you need to shift up the highlighted cell by sending UP key.

    By that code, the active cell becomes the next one of the current active cell when you hit ENTER, and if the current active cell resides on last column (column5), first cell on the next row (the row below the current row) is highlighted as well.

    This should work as i hope.

    HTH.

     

     


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları'ndayım.

    Microsoft Haber Grupları Profilim (VB.NET)

  • Wednesday, February 23, 2011 12:33 AM
     
     
    Thanks I'll try this.
  • Sunday, April 08, 2012 8:31 AM
     
      Has Code

    Here is one to try that takes into account a cell in edit mode. The code is not mine, got it several years ago off the web.

    If this works for you then note the first line of code in Form Load, it defines three columns, this is simply a simple method of defining three columns.

    Public Class Form1
        Private EditRow As Integer = -1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "First Name"
            DataGridView1.Columns(0).DataPropertyName = "Object"
            DataGridView1.Columns(1).Name = "Last Name"
            DataGridView1.Columns(2).Name = "Occupation"
            DataGridView1.Rows.Add(New Object() {"Rod", "Stephens", "Nerd"})
            DataGridView1.Rows.Add(New Object() {"Sergio", "Aragones", "Cartoonist"})
            DataGridView1.Rows.Add(New Object() {"Eoin", "Colfer", "Author"})
            DataGridView1.Rows.Add(New Object() {"Terry", "Pratchett", "?"})
        End Sub
        Private Sub DataGridView1_EditingControlShowing( _
            ByVal sender As Object, _
            ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
        Handles DataGridView1.EditingControlShowing
            EditRow = DataGridView1.CurrentRow.Index
        End Sub
        Private Sub DataGridView1_SelectionChanged( _
            ByVal sender As Object, _
            ByVal e As System.EventArgs) _
        Handles DataGridView1.SelectionChanged
            If EditRow >= 0 Then
                Dim new_row As Integer = EditRow
                EditRow = -1
                DataGridView1.CurrentCell = DataGridView1.Rows(new_row).Cells(DataGridView1.CurrentCell.ColumnIndex)
            End If
        End Sub
        Private Sub DataGridView1_KeyDown( _
            ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles DataGridView1.KeyDown
            If e.KeyCode = Keys.Return Then
                If DataGridView1.CurrentRow.Index < DataGridView1.RowCount - 1 AndAlso _
                    DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1 Then
                    DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.CurrentRow.Index + 1)
                Else
                    Dim cur_cell As DataGridViewCell = DataGridView1.CurrentCell
                    Dim col As Integer = cur_cell.ColumnIndex
                    col = (col + 1) Mod DataGridView1.Columns.Count
                    cur_cell = DataGridView1.CurrentRow.Cells(col)
                    DataGridView1.CurrentCell = cur_cell
                End If
                e.Handled = True
            End If
        End Sub
    End Class


    KSG

  • Monday, April 09, 2012 10:33 AM
     
      Has Code

    Still two problems.   The first is you have to press Enter twice--first to get through SelectedChanged and second to get through KeyDown.   I can't figure out how to do it with just one Enter press.

    The second is that it bombs at the end of the grid if you can't add a row.   I fixed it with an ElseIf

    ElseIf DataGridView1.CurrentRow.Index =_ DataGridView1.RowCount - 1_
    AndAlso DataGridView1.CurrentCell.ColumnIndex =_ DataGridView1.ColumnCount - 1 Then
               DataGridView1.CurrentCell = DataGridView1(0, 0)

  • Monday, April 09, 2012 3:45 PM
     
      Has Code

    Try the following DataGridView instead of the stock DataGridView which was taken from here. I tested it using the same method of loading as in my original reply. Add the class to your project, build then replace the current DataGridView with this DataGridView, build and run.

    Public Class deriveddgv
        Inherits DataGridView
        Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean ' Extract the key code from the key value. 
            Dim key As Keys = keyData And Keys.KeyCode
            ' Handle the ENTER key as if it were a tab ARROW key
            If key = Keys.Enter Then
                Return Me.ProcessTabKey(keyData)
            End If
            Return MyBase.ProcessDialogKey(keyData)
        End Function
        Protected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
            ' Handle the ENTER key as if it were a tab key. 
            If e.KeyCode = Keys.Enter Then
                Return Me.ProcessTabKey(e.KeyData)
            End If
            Return MyBase.ProcessDataGridViewKey(e)
        End Function
    End Class


    KSG