Focus to another column when ComboBox in DataGridView is click or key pressed

n70n1

Member
Joined
Oct 1, 2012
Messages
8
Programming Experience
3-5
Hi all.. how to focus to next cell (or other column in the same row), when combobox in datagridview was clicked or enter key is pressed by user ?

Thanks
 
First, a bit of background on the workings of the DataGridView. Many people don't realise that, by default, it doesn't contain any child controls. Performance would be terrible if it did. What you see in each cell is basically just a drawing of a control. In order to edit the contents of a cell you must begin an editing session in that cell. This can happen in various ways, depending on the value of the grid's EditMode property.

When an editing session starts, the grid creates a control of the appropriate type, or uses an existing one if available, and embeds that control in the cell. At that point the Value property of the cell is assigned to the appropriate property of the editing control, e.g. Text or SelectedValue. The user then edits the contents of the control just as they would for a stand-alone version of the same type. When the editing session ends, which occurs when the user navigates away from that cell or can happen in other ways, the appropriate property of the control is assigned back to the Value of the cell and the control is removed.

When the grid displays the editing control it raises its EditingControlShowing event. That allows you to gain access to the editing control itself. That can be important because, while it has focus, the grid doesn't raise any events. When the editing session ends, the grid raises its CellEndEdit event, allowing you to remove any event handlers that you added to the editing control.

Here's a quick example of how you can handle the SelectionChangeCommitted event of a ComboBox in a DataGridView, indicating that the user has made a selection from the drop-down list:
Private WithEvents editingComboBox As ComboBox

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    editingComboBox = TryCast(e.Control, ComboBox)
End Sub

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    editingComboBox = Nothing
End Sub

Private Sub editingComboBox_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles editingComboBox.SelectionChangeCommitted
    '...
End Sub
The fact that the field is declared WithEvents means that you can use a Handles clause on the SelectionChangeCommitted event handler, so you don't have to use AddHandler and RemoveHandler. The TryCast means that the field will be set to Nothing if the editing control is not a ComboBox. You can modify the logic as required for your needs.

You can then add whatever logic you need to the editing control's event handler(s). If you want to set the CurrentCell of the grid to the next cell then you can do that.
 
You can also add that as standard behaviour for your custom control, that you started with in previous thread http://www.vbdotnetforums.com/winfo...-next-cell-column-when-enter-key-pressed.html
The code is very similar to what jmcilhinney just posted, only using the protected overrides instead of external event handling:
    Private WithEvents editingComboBox As ComboBox

    Protected Overrides Sub OnEditingControlShowing(e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
        MyBase.OnEditingControlShowing(e)
        editingComboBox = TryCast(e.Control, ComboBox)
    End Sub

    Protected Overrides Sub OnCellEndEdit(e As System.Windows.Forms.DataGridViewCellEventArgs)
        MyBase.OnCellEndEdit(e)
        editingComboBox = Nothing
    End Sub

    Private Sub editingComboBox_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles editingComboBox.SelectionChangeCommitted
        MyBase.ProcessTabKey(Keys.Tab)
    End Sub

To move to next cell you can call the internal ProcessTabKey method as in code. Externally that would probably be simplest done calling SendKeys.Send("{TAB}").
 
Thanks very much all Master...

@jmcilhinney :
Can you help me again Sir, I am try to using event keypress or keydown, but can not focus to the next cell, and must be click the list on combobox and enter on the list.
 
Last edited:
Back
Top