Solved

Navigating the cursor position in a DataGridView?

Posted on 2013-12-10
10
1,565 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…

631 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