Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Navigating the cursor position in a DataGridView?

Posted on 2013-12-10
10
Medium Priority
?
1,644 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 2000 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
Independent Software Vendors: 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!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

730 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