获取 DataGridViewComboboxColumn SelectedValue (VB.Net)
我需要获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不必要时最好避免使用全局变量。
在尝试访问comboBox的属性之前,您只需要测试comboBox是否什么都没有:
在我看来,当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
: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 oldcomboBox
, 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.尝试检查
comboBox.SelectedItem.ToString
而不是comboBox.SelectedValue.ToString
希望有帮助。
Try checking for
comboBox.SelectedItem.ToString
instead ofcomboBox.SelectedValue.ToString
Hope that helps.
我有同样的问题。通过对代码进行一些小的更改来解决。
声明一个全局变量
I have the same issue. Sorted out by making small changes in the codes.
Declare a global variable