Solved

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

Posted on 2010-09-15
14
3,424 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now