Solved

Navigating the cursor position in a DataGridView?

Posted on 2013-12-10
10
1,386 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 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