Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4055
  • Last Modified:

VB.NET - Datagridview - Cannot set current cell to first row of next column

Hello,

I have a simple form with 1 datagridview that has 2 columns.  If I'm at the end of the first column and I press enter, I want to set the current cell to the first row of the next column.  Below is my code.  For some reason, the focus keeps moving to the 2nd row, and not the 1st.  Am I missing something?

Public Class Form1


    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then
            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                DataGridView1.CurrentCell = DataGridView1(1, 0)
            End If
        End If
    End Sub

   
End Class
0
jenn2157
Asked:
jenn2157
  • 8
  • 6
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
DataGridView1.CurrentCell = DataGridView1(1, 0)

should be

'ROW THEN COLUMN.
DataGridView1.CurrentCell = DataGridView1(0, 1)
0
 
Jared_SCommented:
Try this

Public Class Form1


    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then
            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                DataGridView1.CurrentCell = DataGridView1.Item(1, 0)
            End If
        End If
    End Sub

    
End Class

Open in new window

0
 
jenn2157Author Commented:
Ged325,
Microsoft states it's column, then row.  I still tried but it didn't work.
http://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx

Jared,
I tried that too but no luck.  I set some break points and it goes to the first row but the cellLeave event fires right away, not sure why??
0
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!

 
Jared_SCommented:
I would think that's just firing because the focus is changing via your keydown event.
0
 
jenn2157Author Commented:
The leave event fires from my current cell (column 0, row 3), then the rowenter for the cell I'm trying to get to (column 1, row 0) then the cellEnter but right away the cellLeave fires for column 1, row 0 and then it's the cellEnter for column 1, row 1.  This actually happens for any cell I try and set current- it goes to the next row... So frustrating!
0
 
Jared_SCommented:
Ah, the joys of programming....
 If you'd like, can you post your code so I can try to recreate it?
0
 
jenn2157Author Commented:
The code I posted was literally it!  I created a windows form program, added a datagridview, used the GUI to add 2 columns with default settings, and added the code above for the keydown event.  I had a more complex program, and simplified it as much as I could to troubleshoot.  Now I'm stuck.  I'm trying to enter data column by column.  The keysow
0
 
jenn2157Author Commented:
The keydown event was to trap the enter key on last row and move to the top of the next column, except it's moving to the row +1 of the next column no matter what cell location I pick.
0
 
Jared_SCommented:
This will do what I think you're after.

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged

        Const allowedRows As Integer = 5

        If DataGridView1.RowCount <= allowedRows Then

            DataGridView1.AllowUserToAddRows = True

        Else

            If DataGridView1.CurrentCell.ColumnIndex = 0 Then

                DataGridView1.CurrentCell = DataGridView1.Item(1, 0)

            ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then

                If DataGridView1.CurrentCell IsNot Nothing And DataGridView1.CurrentRow.Index = allowedRows Then

                    DataGridView1.CurrentCell = DataGridView1.Item(0, 0)

                End If

            End If

        End If

    End Sub

Open in new window

0
 
jenn2157Author Commented:
Kind of- I do not have a set number of rows.  Odd that setting the current cell works in this event, and not the actual keydown event.  I will do some more testing and get back to you.  Thanks for the start.
0
 
Jared_SCommented:
I must have misunderstood what you were doing.

trap the enter key on last row and move to the top of the next column

Is this just one row?
0
 
jenn2157Author Commented:
No, sorry if I was unclear.  It's difficult because typing is subject to interpretation :)

There are unlimited rows, 2 columns.  If I don't enter anything and just press enter when I have run out of data, I want to go to the first row of the next column.
0
 
Jared_SCommented:
Okay...

Going back to your original code, just tweak it a little...

Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

        If DataGridView1.CurrentCell.Value Is Nothing Then

            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                DataGridView1.CurrentCell = DataGridView1(1, 0)
                e.Handled = True
            End If

        End If

    End Sub

Open in new window

0
 
jenn2157Author Commented:
Oh my goodness.  I knew it was going to be something stupidly simple.  Thank you so much for your persistence.  It is GREATLY appreciated!
0
 
jenn2157Author Commented:
Jared stuck with me and provided a simple yet accurate solution.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now