Datagridview move to next column after cell editing
Hello,
I already have a solution to move to the next cell in my datagridview and works fine
I am using this code:
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
' Your code here
SendKeys.Send("{TAB}")
e.SuppressKeyPress = True
End If
End Sub
But after editing in a cell and by press on enter, then the focus goes to the next row.
Thanks in advance,
Marc.
Re: Datagridview move to next column after cell editing
That’s because you’re not editing the cell, you’re editing the dgv editingcontrol.
You need to handle the _editingcontrolshowing event, and set a form level Boolean variable to true, then handle the ProcessCmdKey event, if your form level Boolean is true - catch the enter key, process that however you choose, then reset your form level Boolean to false.
Re: Datagridview move to next column after cell editing
Anyway thanks Paul,
This was not what i was looking for...
I jus found the solution that is working for me,
I'll post this here in case anyone could use it
Test OK - By pressing ENTER go to next COLUMN in your DATAGRIDVIEW
Public Class Form1
Private WithEvents m_EditingControl As DataGridViewTextBoxEditingControl
+++++++
#Region "ENTER key werking als TAB"
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
' Your code here
SendKeys.Send("{TAB}")
e.SuppressKeyPress = True
End If
End Sub
' Set the editing control so we can catch its events.
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
'Debug.WriteLine("Editing")
m_EditingControl = e.Control
End Sub
' Unset the editing control so we no longer catch its events.
Private Sub m_EditingControl_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_EditingControl.LostFocus
m_EditingControl = Nothing
' MessageBox.Show("Done")
SendKeys.Send("{UP}")
End Sub
#End Region
Re: Datagridview move to next column after cell editing
Two things:
1) While it's not wrong to declare that field as type DataGridViewTextBoxEditingControl, you're not using any members of that type so you don't need to get that specific. In your case, even Control would be sufficient, as you're not even using members of TextBox. In fact, if you have any columns that are not text boxes, the code you have will crash when you try to edit one of them, because the editing control will not be that type.
2) As the documentation says, application developers should not be touching the Focus, GotFocus and LostFocus members. In your case, you should be handling the Leave event. In fact, you might even find that you don't need to refer to the editing control at all and you can just handle the CellEndEdit event of the grid. Not sure about that though.
Re: Datagridview move to next column after cell editing
Thanks for your information,
Ill will try this according to your post, and I'll come back after testing the code.
Re: Datagridview move to next column after cell editing
Thanks for your information,
I modified the code and worked with "CellEndEdit" and it works great,
Thanks again.
Below the code i used.
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
SendKeys.Send("{TAB}")
SendKeys.Send("{UP}")
End Sub