Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3959
  • Last Modified:

vb.net 2010 when press enter in datagridview move to next column

I need when press enter  in datagridview move to right column and not to next line. Windows forms.
0
rflorencio
Asked:
rflorencio
  • 11
  • 3
1 Solution
 
John (Yiannis) ToutountzoglouCommented:
Hi ....
Try this
Private Sub MyDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyDataGridView.KeyDown
        If e.KeyCode = Keys.Return Then
            Dim numCols As Integer = MyDataGridView.ColumnCount
            Dim numRows As Integer = MyDataGridView.RowCount
            Dim currCell As DataGridViewCell = MyDataGridView.CurrentCell
            If currCell.ColumnIndex = numCols - 1 Then
                If currCell.RowIndex < numRows - 1 Then
                    MyDataGridView.CurrentCell = MyDataGridView.Item(0, currCell.RowIndex + 1)
                End If
            Else
                MyDataGridView.CurrentCell = MyDataGridView.Item(currCell.ColumnIndex, currCell.RowIndex + 1)
            End If
            e.Handled = True
        End If
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouCommented:
very Sorry try this(this is for rows)
Just be sure to change the selection mode to Cell Select

Private Sub MyDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyDataGridView.KeyDown
        If e.KeyCode = Keys.Return Then
            Dim numCols As Integer = MyDataGridView.ColumnCount
            Dim numRows As Integer = MyDataGridView.RowCount
            Dim currCell As DataGridViewCell = MyDataGridView.CurrentCell
            If currCell.ColumnIndex = numCols - 1 Then
                If currCell.RowIndex < numRows - 1 Then
                    MyDataGridView.CurrentCell = MyDataGridView.Item(0, currCell.RowIndex + 1)
                End If
            Else
                MyDataGridView.CurrentCell = MyDataGridView.Item(currCell.ColumnIndex+1, currCell.RowIndex)
            End If
            e.Handled = True
        End If
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouCommented:
Also you have to set the edit Mode on EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
John (Yiannis) ToutountzoglouCommented:
Finally my first post is for moving to the next Row and the second is to move in columns cell by cell
0
 
rflorencioAuthor Commented:
Not work, when press enter cursor move to next row, with selection mode to cell select  and edit mode to editonenter
0
 
John (Yiannis) ToutountzoglouCommented:
that is why i said edit on Keystroke....
you want it with edit on enter?Ans also try the second code
0
 
John (Yiannis) ToutountzoglouCommented:
Did you try the code?
0
 
rflorencioAuthor Commented:
yes, both, but when press enter cursor move to next row and not to next column in same row.

properties set:
Selectionmode=  Cell Select
editmode=EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
 
John (Yiannis) ToutountzoglouCommented:
i thing you are talking about pressing enter when the cell is EditMode.and not just navigate through the cells.Correct?
0
 
John (Yiannis) ToutountzoglouCommented:
0
 
John (Yiannis) ToutountzoglouCommented:
It is working i test it in my datagridview...
1.create a custom control and put this code :

Public Class Mydatagrid
    Inherits DataGridView

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

        If key = Keys.Enter Then
            MyBase.OnKeyDown(New KeyEventArgs(keyData))
            Return True
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function

End Class


'Here is the differnce the ProcessDialogKey...
then drag the MyDatagrid Custom Control into Your Form (if it is bounded set again the datasource )
Then in your Form Put these Lines:
 Private Sub Mydatagrid1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Mydatagrid1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim ri As Integer = Mydatagrid1.CurrentCell.RowIndex
            Dim ci As Integer = Mydatagrid1.CurrentCell.ColumnIndex
            e.SuppressKeyPress = True
            FindNextCell(Mydatagrid1, ri, ci + 1)  'checking from Next  
        End If
    End Sub

Sub FindNextCell(ByVal dgv As Mydatagrid, ByVal rowindex As Integer, ByVal columnindex As Integer)
        Dim found As Boolean = False

        While dgv.RowCount > rowindex
            While dgv.Columns.Count > columnindex
                If Not (dgv.Rows(rowindex).Cells(columnindex)).ReadOnly Then
                    dgv.CurrentCell = dgv.Rows(rowindex).Cells(columnindex)
                    Exit Sub
                Else
                    columnindex += 1
                End If
            End While
            rowindex += 1
            columnindex = 0
        End While
    End Sub

Open in new window

0
 
rflorencioAuthor Commented:
Still not working, the steps I did were these:
1 - Create a custom control called Mydatagrid
2 - Drag from toolbox new control to form
3 -  Put Sub FindNextCell in form, and in keydown event of new mydatagrid control put the code above
0
 
John (Yiannis) ToutountzoglouCommented:
Strange....it works Perfectly on me....with the same steps....let me check it again...
0
 
John (Yiannis) ToutountzoglouCommented:
I check It ...It Works
Just Copy And Paste the codeas it is
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now