SelectedIndex from DataGridViewComboBoxCell? VB.NET

How can I set the SelectedIndex in the DataGridViewComboBoxCell?

The code fills a combo box with items, but I need to select one of them

My code:

 Dim cListItems As New System.Collections.Generic.List(Of Combobox_values)

                If ds.Tables("items_prices").Rows(0).Item("item_selldozen") > 0 Then
                    Dim item_selldozen As String = ds.Tables("items_prices").Rows(0).Item("item_selldozen")
                    cListItems.Add(New Combobox_values("Docena (" + item_selldozen + ")", item_selldozen))
                End If

                Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(CType(main.ActiveMdiChild, discount_new_discount).discountitems_new_discount.Rows(last_row).Cells(3), DataGridViewComboBoxCell)

                dgvcbc.DataSource = cListItems 'Fill Remote Comboboxcell
                dgvcbc.DisplayMember = "Text"
                dgvcbc.ValueMember = "Value"



source to share

1 answer

If you have a ComboBoxColumn in your DataGridView and you want to know what is the selected index of the combo box, you need to do this:

  • Handle the EditingControlShowing DataGridView event. In this event handler, check if the current column is interested. Then we create a temporary ComboBox object and get the selected index:

Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
    If dataGridView1.CurrentCell.ColumnIndex = 0 Then
        ' Check box column
        Dim comboBox As ComboBox = TryCast(e.Control, ComboBox)
        comboBox.SelectedIndexChanged += New EventHandler(AddressOf comboBox_SelectedIndexChanged)
    End If
End Sub

Private Sub comboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim selectedIndex As Integer = DirectCast(sender, ComboBox).SelectedIndex
    MessageBox.Show("Selected Index = " & selectedIndex)
End Sub




All Articles