Solved

Navigating the cursor position in a DataGridView?

Posted on 2013-12-10
10
1,317 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now