1

When you add a ComboBoxColumn in DataGridView, I do not know how to handle the change event of ComboBox.

'Adding To DGV data on form load
Dim cmbovoce As New DataGridViewComboBoxColumn()
cmbovoce.HeaderText = "Fruit"
cmbovoce.Name = "cmbovoce"
cmbovoce.MaxDropDownItems = 4
cmbovoce.Width = 100
cmbovoce.Items.Add("apple")
cmbovoce.Items.Add("pear")
cmbovoce.Items.Add("cherries")
cmbovoce.Items.Add("plums")
DataGridView1.Columns.Add(cmbovoce)
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Mubby
  • 95
  • 1
  • 12
  • 3
    the code looks like it is working with a standard DGV ComboBox column (not a listbox), but what do you mean by the "trigger event"? If you are manually adding a column, you need to hook up the event handlers you will be using. – Ňɏssa Pøngjǣrdenlarp Sep 01 '15 at 12:00
  • I'm sorry, I mistook ComboBox with ListBox. I need that when you change the items in the ComboBox I call a function with which sends id line and a new index ComboBox in that row (first item returns 0, the second 1 ..) – Mubby Sep 01 '15 at 13:16
  • @RezaAghaei, yep and i have it. – Mubby Sep 02 '15 at 15:52

1 Answers1

1

I strongly recommend you to use Cell events, such as CellValidating, CellValueChanged, ... to detect changes. The combobox that you are trying to handle its SelectedIndexChange event, is a unique instance for all cells.

Anyway, if you want to know how to handle SelectedIndexChange event of it,you can do it this way:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim c = New DataGridViewComboBoxColumn()
    c.HeaderText = "Fruit"
    c.Name = "c"
    c.MaxDropDownItems = 4
    c.Width = 100
    c.Items.Add("apple")
    c.Items.Add("pear")
    c.Items.Add("cherries")
    c.Items.Add("plums")
    Me.DataGridView1.Columns.Add(c)

    For index = 1 To 5
        Me.DataGridView1.Rows.Add()
    Next
End Sub

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If (TypeOf (e.Control) Is ComboBox) Then
        Dim combo = CType(e.Control, ComboBox)
        RemoveHandler combo.SelectedIndexChanged, AddressOf c_SelectedIndexChanged
        AddHandler combo.SelectedIndexChanged, AddressOf c_SelectedIndexChanged
    End If
End Sub

Private Sub c_SelectedIndexChanged(sender As Object, e As EventArgs)
    If (Me.DataGridView1.Columns(Me.DataGridView1.CurrentCell.ColumnIndex).Name = "c") Then
        Dim combo = CType(sender, ComboBox)
        'Do something with combo
    End If
End Sub
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398