Solved

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

Posted on 2010-09-15
14
3,485 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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