Get DataGridViewComboboxColumn SelectedValue (VB.N

2020-02-15 09:30发布

I need to get the selected value of a ComboBox in a DataGridView. I have it partially working, but I get a Null Reference Exception if I change a another ComboBox in the grid. Here's my code:

Private Sub dgvSampleList_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvSampleList.EditingControlShowing
    Dim comboBox As ComboBox = CType(e.Control, ComboBox)

    If (comboBox IsNot Nothing) Then
        'Remove an existing event-handler
        RemoveHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

        'Add the event handler. 
        AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
    End If
End Sub

Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim comboBox As ComboBox = CType(sender, ComboBox)
    'Display selected value
    MsgBox("ProgramID: " & comboBox.SelectedValue.ToString)
End Sub

This works fine the first time the ComboBox is changed, but generates a Null Reference Exception if another ComboBox is changed. Any ideas why this is happening? Note: I found most this code on MSDN's discussion forms.

Thanks!

Peter

3条回答
smile是对你的礼貌
2楼-- · 2020-02-15 09:53

Try checking for comboBox.SelectedItem.ToString instead of comboBox.SelectedValue.ToString

Hope that helps.

查看更多
够拽才男人
3楼-- · 2020-02-15 09:54

It's best to avoid global variables when they are unnecessary.

You just need to test for whether comboBox is nothing before trying to access a property of comboBox:

Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim comboBox As ComboBox = CType(sender, ComboBox)
    'Display selected value
    If comboBox IsNot Nothing Then
        MsgBox("ProgramID: " & comboBox.SelectedValue.ToString)
    End If
End Sub

It seems to me that when the comboBox is set from an old value to the new value, that this SelectedIndexChanged event gets called for both the old and new comboboxes. I suspect that when it gets called for the old comboBox, the sender is null/Nothing because its value is getting changed. Maybe. But no matter what it is happening, a null is a null. Just test that it's not null before you try to access any of its properties.

查看更多
狗以群分
4楼-- · 2020-02-15 10:06

I have the same issue. Sorted out by making small changes in the codes.

Declare a global variable

Dim comboBoxCol As New DataGridViewComboBoxColumn
Dim gol As Integer = 0



 Dim comboBox As ComboBox
    Private Sub dgvSampleList_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DGVItems.EditingControlShowing
        comboBox = CType(e.Control, ComboBox)

        If (comboBox IsNot Nothing) Then

            'Add the event handler.  
            AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
            gol = 1
            'AddHandler comboBox.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
        End If
    End Sub

    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        comboBox = CType(sender, ComboBox)
        If gol = 1 Then
            Dim item As String = comboBox.Text
            MsgBox(item)
            gol = 0
        End If
  End Sub
查看更多
登录 后发表回答