Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectedIndex of a DataGridViewComboBoxCell? VB.NET

How do I set SelectedIndex of a DataGridViewComboBoxCell?

The code fill the combobox 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"
like image 403
John Nuñez Avatar asked Nov 19 '25 10:11

John Nuñez


1 Answers

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

  1. Handle the EditingControlShowing event of DataGridView. In this event handler, check if the current column is of our interest. 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
like image 150
Kishore Kumar Avatar answered Nov 21 '25 23:11

Kishore Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!