Solved

Moving to the next Column in a DGV using Enter Key???

Posted on 2007-04-04
18
364 Views
Last Modified: 2012-05-05
How can I advance to the next column, same row of a DGV using the Enter/Return Key?
0
Comment
Question by:BlakeMcKenna
  • 8
  • 7
  • 3
18 Comments
 
LVL 5

Expert Comment

by:mytonytiger
ID: 18854089
You can capture the key down event of the datagridview. The programatically select the next cell

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

        If e.KeyCode = Keys.Return Then
            e.Handled = True
            'Code to select next cell goes here


        End If

    End Sub
0
 
LVL 5

Expert Comment

by:mytonytiger
ID: 18854111
Here's one example of moving to the next cell:

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

        If e.KeyCode = Keys.Return Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If

    End Sub
0
 

Author Comment

by:BlakeMcKenna
ID: 18854172
That doesn't work. When I hit the Return/Enter Key, I just stay in the same cell.
0
 
LVL 5

Expert Comment

by:voordes
ID: 18854235
Hello,

Try this. Tested it just now and it worked. Might need some tweaking if the active cell is in edit mode.

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        'Check what was pressed and act accordingly
        If e.KeyCode = Keys.Enter Then
            'Resolve the next column index that is visible or it reached the end
            Dim index As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
            Do Until index > (DataGridView1.ColumnCount - 1) OrElse DataGridView1.Columns(index).Visible = True
                index = index + 1
            Loop
            If index > (DataGridView1.ColumnCount - 1) Then
                index = DataGridView1.CurrentCell.ColumnIndex
            End If
            'Activate the new index
            DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells(index)
            'Tell the grid that you handled the key
            e.Handled = True
        End If

    End Sub


Grtz,
SV
0
 

Author Comment

by:BlakeMcKenna
ID: 18854278
It's still not working! Still stays in the same cell.
0
 

Author Comment

by:BlakeMcKenna
ID: 18854282
Here's my code:

    Private Sub grd_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd.CellEnter
        Try
            grd.BeginEdit(True)

            Select Case e.ColumnIndex
                Case 2
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 3
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 4
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 5
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 6
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 7
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
                Case 8
                    grd.Columns(e.ColumnIndex).DefaultCellStyle.Format = "##.00"
            End Select

            If e.ColumnIndex = 0 Then
                btnDelete.Enabled = True
            Else
                btnDelete.Enabled = False
            End If

        Catch ex As Exception
            strProcedure = "grd_CellEnter()"
            Msg = ex.Message
            ErrorHandler(strModule, strProcedure, Msg)
        End Try
    End Sub

    Private Sub grd_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd.CellLeave
        Try
            SumCell()

            Select Case e.ColumnIndex
                Case 2
                    grd.CurrentRow.Cells(2).Value = Format(grd.CurrentRow.Cells(2).Value, "##.00")
                Case 3
                    grd.CurrentRow.Cells(3).Value = Format(grd.CurrentRow.Cells(3).Value, "##.00")
                Case 4
                    grd.CurrentRow.Cells(4).Value = Format(grd.CurrentRow.Cells(4).Value, "##.00")
                Case 5
                    grd.CurrentRow.Cells(5).Value = Format(grd.CurrentRow.Cells(5).Value, "##.00")
                Case 6
                    grd.CurrentRow.Cells(6).Value = Format(grd.CurrentRow.Cells(6).Value, "##.00")
                Case 7
                    grd.CurrentRow.Cells(7).Value = Format(grd.CurrentRow.Cells(7).Value, "##.00")
                Case 8
                    grd.CurrentRow.Cells(8).Value = Format(grd.CurrentRow.Cells(8).Value, "##.00")
            End Select

        Catch ex As Exception
            strProcedure = "grd_CellLeave()"
            Msg = ex.Message
            ErrorHandler(strModule, strProcedure, Msg)
        End Try
    End Sub

    Private Sub grd_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd.CellValueChanged
        Try
            Select Case e.ColumnIndex
                Case 2, 3, 4, 5, 6, 7, 8
                    grd.CurrentRow.Cells(9).Value = CType(grd.Rows(e.RowIndex).Cells(2).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(3).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(4).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(5).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(6).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(7).Value, Integer) + _
                                                    CType(grd.Rows(e.RowIndex).Cells(8).Value, Integer)
            End Select

        Catch ex As Exception
            strProcedure = "grd_CellValueChanged()"
            Msg = ex.Message
            ErrorHandler(strModule, strProcedure, Msg)
        End Try
    End Sub

    Private Sub grd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grd.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                'Resolve the next column index that is visible or it reached the end
                Dim index As Integer = grd.CurrentCell.ColumnIndex + 1
                Do Until index > (grd.ColumnCount - 1) OrElse grd.Columns(index).Visible = True
                    index = index + 1
                Loop
                If index > (grd.ColumnCount - 1) Then
                    index = grd.CurrentCell.ColumnIndex
                End If
                'Activate the new index
                grd.CurrentCell = grd.CurrentRow.Cells(index)
                'Tell the grid that you handled the key
                e.Handled = True
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class
0
 
LVL 5

Expert Comment

by:voordes
ID: 18854313
Ok, Lot of code to interprete there;) Could you tell me if it works when you temporariliy remove all other events on the grid. While you're foing so, I'll have another look at your code.

SV
0
 
LVL 5

Expert Comment

by:voordes
ID: 18854338
Another question: If you take out the try catch constructions could it be you run into exceptions. If so this could interupt the overall flow of the app.

SV
0
 

Author Comment

by:BlakeMcKenna
ID: 18854344
I did comment out the rest of the Grid Events and it still bombed. If I have multiple rows on the grid, which I do, it just goes to the next row, same column. I want it to move to the next column SAME row.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:BlakeMcKenna
ID: 18854352
I tried commenting out the Try Catch's and that didn't effect anything.
0
 
LVL 5

Expert Comment

by:voordes
ID: 18854386
Hmm, I pasted your code into my project and found it to act quite reluctantly. What boggled me a little was why you perform the column setup in the cell_enter event. Wouldn't it suffice to do this once during the formload for instance. Same goes for the cell_leave; what are you aiming for there?

Anyway, I think the mentioned events are interfering somehow here. As I mentioned before, my earlier solution does not work correctly when in edit mode. Looking into that but the problem is that when leaving editmode with the enter key the keypress and keydown events are ignored and I can't tell it that I handled the key already, so it just moves down. Here's the new code:

   Private Sub grd_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd.CellEndEdit
        'Call the move to next cell routine
        MoveToNextCell()
    End Sub

    Private Sub MoveToNextCell()
        'Resolve the next column index that is visible or it reached the end
        Dim index As Integer = grd.CurrentCell.ColumnIndex + 1
        Do Until index > (grd.ColumnCount - 1) OrElse grd.Columns(index).Visible = True
            index = index + 1
        Loop
        If index > (grd.ColumnCount - 1) Then
            index = grd.CurrentCell.ColumnIndex
        End If
        'Activate the new index
        grd.CurrentCell = grd.CurrentRow.Cells(index)
    End Sub

    Private Sub grd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grd.KeyDown
        'Check what was pressed and act accordingly
        If e.KeyCode = Keys.Enter Then
            'Call the cell move routine
            MoveToNextCell()
            'Tell the grid that you handled the key
            e.Handled = True
        End If
    End Sub

Going now, good luck and maybe till tomorrow.
SV
0
 

Author Comment

by:BlakeMcKenna
ID: 18854428
What I am trying to do is simple...at least in concept. I have a grid that is basically a timesheet.  It has 9 columns, 7 of which are the days of the week; mon - sun. In the mon - sun columns, I want to enter a number representing hours worked.  So, if I enter a number like 5, I want it to be fomatted like 5.00 or if I enter 6.5, have it come out as 6.50.  Each time I leave a cell, I want to update the entire grid specifically the 7 day columns as well as a Total Column.

That's all I want to do. Of course updating this grid will update a database table as well.
0
 
LVL 5

Expert Comment

by:voordes
ID: 18861422
Well, I think the best approach here is to set up a good dataset and table and bind the grid to it. In this dataset set up the columns of the weekdays with datatype double. By doing so it will correctly apply the format to the underlying value.

In the grid set up the columns with format being #.##0.00. You can do this in design time. Now, as for the calculation, I would add an extra column to the datatable with an expression that sums the hours. This would be something like:
D1+D1+D3+D4+D5+D6+D7
(assuming the columns would be called D1, D2 etc)

As a result any change to a value in one of the other columns is automaticly calculated to the summation column. In my opinion a 'cleaner' solution.

Back to the original question on moving one cell to the right. My previously posted comment provides for half the functionality but still doesn't work when a cell is in edit mode. I'm still working on that one.

Success,
SV
0
 

Author Comment

by:BlakeMcKenna
ID: 18861626
SV,

I've actually got the grid working the way I want (calculations of all columns/rows). However, I'm still not getting the cursor to advance to the next column upon hitting enter/return.
0
 
LVL 5

Expert Comment

by:voordes
ID: 18879433
Just for clarification, are the calculations done through events or through the expression property now?

As for the movigint to the next column. The earlier presented code should really do the trick. Although when you press enter while being in ener mode it will (unfortunately) still move downward but with the event triggerd also move to the right. I still am  not able to avoid it from moving down after leaving edit mode.

Keep me posted, as I will do too.
SV
0
 

Author Comment

by:BlakeMcKenna
ID: 18882378
thanks for the tidbit!
0
 
LVL 5

Expert Comment

by:mytonytiger
ID: 18884206
The code I posted also moves to the next cell when not in edit mode.
0
 
LVL 5

Accepted Solution

by:
voordes earned 500 total points
ID: 18886005
Well, at least I'm confused on the status now. Who's gettting what to work and what not? As far as I know the KeyDown event is not triggered when a cell is in edit mode, which makes it impossible to do something with the enter key in those cases. Only thing I could suggest is to set some variable upon leaving edit mode and to forward to another cell in something like the cellenter event when this variable is set.

SV
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

11 Experts available now in Live!

Get 1:1 Help Now