首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SelectedIndex of a DataGridViewComboBoxCell?VB.NET

SelectedIndex of a DataGridViewComboBoxCell?VB.NET
EN

Stack Overflow用户
提问于 2012-03-21 17:14:42
回答 1查看 8.7K关注 0票数 3

如何设置SelectedIndex的DataGridViewComboBoxCell?

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

我的守则:

代码语言:javascript
复制
 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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-21 17:26:17

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

  1. 处理DataGridView的EditingControlShowing事件。在此事件处理程序中,检查当前列是否我们感兴趣。然后创建一个临时的ComboBox对象,并获得所选的索引:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9809561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档