获取“ DataGridViewComboBox”的选定索引 [英] Get selected index of `DataGridViewComboBox`

查看:419
本文介绍了获取“ DataGridViewComboBox”的选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在 DataGridView 中添加 ComboBoxColumn 时,我不知道如何处理 ComboBox

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)


推荐答案

我强烈建议您使用单元事件,例如 CellValidating CellValueChanged 等来检测更改。您尝试处理其 SelectedIndexChange 事件的组合框是所有单元格的唯一实例。

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.

如果您想知道如何处理它的 SelectedIndexChange 事件,可以这样操作:

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

这篇关于获取“ DataGridViewComboBox”的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆