Solved

Navigating the cursor position in a DataGridView?

Posted on 2013-12-10
10
1,430 Views
Last Modified: 2013-12-12
I have a DataGridView in which the User can enter data in certain cells. In these specific cells, when they have entered a correct value and hit enter, the next cell to have the focus should be to the right of the previous cell and not below it. Usually in a Grid, when you hit the enter key, the cursor advances to the next row, I need it to advance to the next column. How can I correctly do that programmatically?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 6
  • 4
10 Comments
 

Author Comment

by:BlakeMcKenna
ID: 39711773
Can anyone help me with this??
0
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 39712193
I worked on some code yesterday after seeing your question, but I have not had time to fully test it.  Here is what I was able to come up with.  This should get you started.  Add the code in the events like below.

    Private Sub DataGridView2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView2.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            Dim iCol = DataGridView2.CurrentCell.ColumnIndex
            Dim iRow = DataGridView2.CurrentCell.RowIndex
            If iCol = DataGridView2.Columns.Count - 1 Then
                If iRow < DataGridView2.Rows.Count - 1 Then
                    DataGridView2.CurrentCell = DataGridView2(0, iRow + 1)
                End If
            Else
                DataGridView2.CurrentCell = DataGridView2(iCol + 1, iRow - 1)
            End If
        End If

    End Sub

    Private Sub DataGridView2_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellEndEdit
        Dim iCol = DataGridView2.CurrentCell.ColumnIndex
        Dim iRow = DataGridView2.CurrentCell.RowIndex
        If iCol = DataGridView2.Columns.Count - 1 Then
            If iRow < DataGridView2.Rows.Count - 1 Then
                DataGridView2.CurrentCell = DataGridView2(0, iRow + 1)
            End If
        Else
            If iRow < DataGridView2.Rows.Count - 1 Then
                SendKeys.Send("{up}")
            End If
            DataGridView2.CurrentCell = DataGridView2(iCol + 1, iRow)
        End If

    End Sub

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 39712576
I can't get this to work. It's not doing anything different. It also appears that the KeyPress Event isn't firing...not sure why about that one!
0
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.

 

Author Comment

by:BlakeMcKenna
ID: 39712598
Sorry...it does work. I forgot to rename the DataGridView2 to the name I was using.
0
 

Author Comment

by:BlakeMcKenna
ID: 39712851
I jumped a little too soon. Everything works fine until I get in the last row. When I enter a value and press the Enter Key, the cell advances to the next cell beside the previous but there is no value now. It loses the value as soon as I hit Enter.
0
 
LVL 9

Expert Comment

by:dustock
ID: 39713977
I'm not seeing the same behavior, try stepping through your code and see if you can find at what point the value is getting removed from the cell.  Maybe you have a different event that fires causing this to happen?
0
 

Author Comment

by:BlakeMcKenna
ID: 39714077
I can't see anywhere that cell actually loses it's value. I don't think it does. Here is the code that I am using for this DGV.

        Private Sub dgvLoadTests_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLoadTests.CellEndEdit
        Try
            Dim cIdx = dgvLoadTests.CurrentCell.ColumnIndex
            Dim rIdx = dgvLoadTests.CurrentCell.RowIndex

            If cIdx = dgvLoadTests.Columns.Count - 1 Then      
                If rIdx < dgvLoadTests.Rows.Count - 1 Then      
                    dgvLoadTests.CurrentCell = dgvLoadTests(0, rIdx + 1)
                End If
            Else
                If rIdx < dgvLoadTests.Rows.Count - 1 Then
                    SendKeys.Send("{up}")
                End If
                dgvLoadTests.CurrentCell = dgvLoadTests(cIdx + 1, rIdx)
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellEndEdit() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub
    '
    '
    '
    Private Sub dgvLoadTests_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvLoadTests.CellMouseClick
        Try
            iRow = dgvLoadTests.CurrentRow.Index

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellMouseClick() - " & ex.Message
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub
    '
    '
    '
    Private Sub dgvLoadTests_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvLoadTests.CellValidating
        Try
            EH.strRetVal = ""

            If dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).IsInEditMode Then
                If Not dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).ValueType Is GetType(Decimal) Then
                    Dim c As Control = dgvLoadTests.EditingControl
                    Dim cell As DataGridViewCell = dgvLoadTests.CurrentCell

                    If Not Decimal.TryParse(c.Text, Nothing) Then
                        If c.Text = "" Then
                        Else
                            EH.strRetVal = "Invalid Numeric Value!" & "~I"
                            e.Cancel = True
                        End If
                    Else
                        cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
                    End If
                    dgvLoadTests.CancelEdit()
                End If
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellValidating() - " & ex.Message
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub
    '
    '
    '
    Private Sub dgvLoadTests_KeyPress(sender As Object, e As KeyPressEventArgs) Handles dgvLoadTests.KeyPress
        Try
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Or e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
                If blnOEM Then
                    Dim cIdx = dgvLoadTests.CurrentCell.ColumnIndex
                    Dim rIdx = dgvLoadTests.CurrentCell.RowIndex


                    If cIdx = dgvLoadTests.Columns.Count - 1 Then          
                        If rIdx < dgvLoadTests.Rows.Count - 1 Then          
                            dgvLoadTests.CurrentCell = dgvLoadTests(0, rIdx + 1)
                        End If
                    Else
                        dgvLoadTests.CurrentCell = dgvLoadTests(cIdx + 1, rIdx)
                    End If
                Else
                End If
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_KeyPress() - " & ex.Message
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub
0
 
LVL 9

Expert Comment

by:dustock
ID: 39714825
How is your datagridvew setup in the properties?  When I wrote this code I left the defaults for the datagridview.  Once I know how your is setup I can try to recreate the scenario as close as possible to what you have.
0
 

Author Comment

by:BlakeMcKenna
ID: 39714949
I got it working now...just needed to make some adjustments. So you code worked! Is this thread still considered closed?
0
 
LVL 9

Expert Comment

by:dustock
ID: 39715016
Cool, glad its working!  I'm pretty sure as soon as you accept the thread is closed so we should be good.  Good luck with your program!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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