如何设置 DataGridViewComboBoxCell 的 SelectedIndex?

代码用项目填充组合框,但我需要选择其中之一

我的代码:

 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"

最佳答案

如果您的 DataGridView 中有一个 ComboBox Column 并且您想知道组合框的选定索引是什么,那么您需要这样做:

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

关于windows - DataGridViewComboBoxCell 的 SelectedIndex?网络,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9809561/

10-11 15:52