获取DataGridViewComboboxColumn SelectedValue(VB.Net)

编程入门 行业动态 更新时间:2024-10-27 19:15:07
本文介绍了获取DataGridViewComboboxColumn SelectedValue(VB.Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在DataGridView中获取ComboBox的选定值。我有部分工作,但是如果更改网格中的另一个ComboBox,则会收到 Null Reference Exception 。这是我的代码:

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

在第一次更改ComboBox时此方法正常,但是如果更改了另一个ComboBox,则生成Null引用异常。任何想法为什么会这样?注意:我在MSDN的讨论表上找到了大部分代码。

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.

谢谢!

Peter

推荐答案

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

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

您只需要在尝试访问 comboBox 的属性之前测试comboBox是否为空:

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

在我看来,当 comboBox 从旧值设置为新值时,这个SelectedIndexChanged事件会同时被调用新的组合框。我怀疑当调用旧的 comboBox 时,发件人为null / Nothing,因为其值正在更改。也许。但是无论发生什么,null都是null。在尝试访问其任何属性之前,只需测试它是否不为空即可。

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.

更多推荐

获取DataGridViewComboboxColumn SelectedValue(VB.Net)

本文发布于:2023-11-03 12:02:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SelectedValue   DataGridViewComboboxColumn   Net   VB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!