?
Solved

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

Posted on 2010-09-15
14
Medium Priority
?
4,090 Views
Last Modified: 2012-08-14
I need when press enter  in datagridview move to right column and not to next line. Windows forms.
0
Comment
Question by:rflorencio
  • 11
  • 3
14 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33679936
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33679944
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33679985
Also you have to set the edit Mode on EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33679999
Finally my first post is for moving to the next Row and the second is to move in columns cell by cell
0
 

Author Comment

by:rflorencio
ID: 33680091
Not work, when press enter cursor move to next row, with selection mode to cell select  and edit mode to editonenter
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680106
that is why i said edit on Keystroke....
you want it with edit on enter?Ans also try the second code
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680314
Did you try the code?
0
 

Author Comment

by:rflorencio
ID: 33680434
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680446
i thing you are talking about pressing enter when the cell is EditMode.and not just navigate through the cells.Correct?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680464
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 2000 total points
ID: 33680520
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
 

Author Comment

by:rflorencio
ID: 33681086
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33682170
Strange....it works Perfectly on me....with the same steps....let me check it again...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33682296
I check It ...It Works
Just Copy And Paste the codeas it is
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question