获取 DataGridViewComboboxColumn SelectedValue (VB.Net)

发布于 2024-11-15 00:40:03 字数 1059 浏览 3 评论 0原文

我需要获取 DataGridView 中 ComboBox 的选定值。我已经部分工作了,但是如果我更改网格中的另一个组合框,我会收到空引用异常。这是我的代码:

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

第一次更改组合框时效果很好,但如果更改另一个组合框,则会生成空引用异常。有什么想法为什么会发生这种情况吗?注意:我在 MSDN 的讨论表单上找到了大部分此类代码。

谢谢!

彼得

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聆听风音 2024-11-22 00:40:03

不必要时最好避免使用全局变量。

在尝试访问comboBox的属性之前,您只需要测试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

在我看来,当comboBox从旧值设置为新值时值,为旧的和新的组合框调用此 SelectedIndexChanged 事件。我怀疑当它被旧的 comboBox 调用时,发送者为 null/Nothing,因为它的值正在改变。或许。但无论发生什么,空就是空。在尝试访问它的任何属性之前,只需测试它是否不为 null。

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.

梦开始←不甜 2024-11-22 00:40:03

尝试检查 comboBox.SelectedItem.ToString 而不是 comboBox.SelectedValue.ToString

希望有帮助。

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

Hope that helps.

凉城已无爱 2024-11-22 00:40:03

我有同样的问题。通过对代码进行一些小的更改来解决。

声明一个全局变量

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文