Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-15
14
Medium Priority
?
3,872 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 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