Kat
Kat

Reputation: 2498

How to commit edit or end edit on selection confirmed for datagridview combobox cell?

Problem: I want to commit an edit for when the user has a datagridview combobox cell, i.e. it would be the regular combo box cell's "Selection Committed"

What I've Tried: So based off a lot of stackflow questions (like Datagridview comboBox not selecting on click/edit ) where they look at the event called such as selected index changed, the cell value changed, cell content click, and edit control showing. In these event handlers I have tried to commit the edit, but it instead calls this event after another cell has clicked, not once the selected item has been chosen in the cell. I've already tried making a "selected index changed" handler and definition, but since the combobox cell doesn't have that event, I get an error in vb.net

Here's a sample code snippet:

 Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    editCameraTable.CommitEdit(DataGridViewDataErrorContexts.Commit)
    editCameraTable.EndEdit()
End Sub

This isn't called until another cell is clicked or otherwise. Any suggestions on how to flag this event for a datagridviewcomboboxcell?

Upvotes: 2

Views: 9011

Answers (1)

Nathan
Nathan

Reputation: 171

Handel the CellEndEdit action. Yes it only fires after the person clicks elsewhere; but combine it with a keydown preview.. People naturally click return at the end of a text edit and you can use this to trigger the update. A Cell in a gridview can't determine when a person is done typing in it, so it needs the person to click out of it before the edit commit occurs. The only other way around this is perhaps using a timer that resets on each keydown event and updates if the typing goes idle.

Private Sub datagridview1_CellEndEdit(ByVal sender As Object, _
               ByVal e As System.EventArgs) HAndels datagridview1.CellEndEdit

   '  If InLoad = True Then Exit Sub ' may want to suppress if you are programmatically changing this

   datagridview1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
   ' Do your saves... 


End Sub

Private Sub datagridview1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
     Handles datagridview1.KeyDown

    If e.KeyCode = Keys.Return Then
        datagridview1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
    End If

End Sub

Upvotes: 0

Related Questions