VB.net DataGridView1_CellValueChanged  - get this to run when ComboBox selection is made

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I have a DataGridView where I add several ComboBox columns programatically.

I need to run code when a ComboBox selection is made but the event DataGridView1_CellValueChanged only runs when I click another cell.

How can I run this code without having to select another cell? Is there another event that
I should rather use?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Why not you add a combo box at form level with events, then you can check the combo box index changed event.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
I can't do that in this case because the columns names have to be the same as changing data
Perhaps DataGridView1.CommitEdit is the way. What code would I use?
It will work here, but you have to handle different situations yourself. Try create a new form and put this codes in, create a datagridview named datagridview1


    Private WithEvents colX As New DataGridViewComboBoxColumn

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        colX.HeaderText = "Display 1"
        colX.Name = "MyColumn1"
        colX.DisplayIndex = 0 'set as the first column in the grid
        colX.Items.Add(1)
        colX.Items.Add(2)
        colX.Items.Add(3)
        DataGridView1.Columns.Add(colX)
    End Sub

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim editingComboBox As ComboBox = e.Control
        AddHandler editingComboBox.SelectedIndexChanged, AddressOf Me.editingComboBox_SelectedIndexChanged
    End Sub

    Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'Handle your codes here to identity correct method and do what you required
        MsgBox("You see me when you changed the value of a combobox")
    End Sub
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial