Solved

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

Posted on 2010-09-15
14
3,314 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
Comment Utility
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
Comment Utility
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
Comment Utility
Also you have to set the edit Mode on EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try the code?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:rflorencio
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Strange....it works Perfectly on me....with the same steps....let me check it again...
0
 
LVL 18

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

10 Experts available now in Live!

Get 1:1 Help Now