[英]Get DataGridViewComboboxColumn SelectedValue (VB.Net)
I need to get the selected value of a ComboBox in a DataGridView.我需要在 DataGridView 中获取 ComboBox 的选定值。 I have it partially working, but I get a Null Reference Exception if I change a another ComboBox in the grid.我让它部分工作,但如果我在网格中更改另一个 ComboBox,我会得到一个Null 参考异常。 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.这在第一次更改 ComboBox 时工作正常,但如果另一个 ComboBox 更改,则会生成 Null 参考异常。 Any ideas why this is happening?任何想法为什么会发生这种情况? Note: I found most this code on MSDN's discussion forms.注意:我在 MSDN 的讨论 forms 中找到了大部分此代码。
Thanks!谢谢!
Peter彼得
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
:在尝试访问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
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.在我看来,当comboBox
从旧值设置为新值时,新旧组合框都会调用此 SelectedIndexChanged 事件。 I suspect that when it gets called for the old comboBox
, the sender is null/Nothing because its value is getting changed.我怀疑当它被调用旧的comboBox
时,发件人是 null/Nothing,因为它的值正在改变。 Maybe.也许。 But no matter what it is happening, a null is a null.但无论发生什么,null 就是 null。 Just test that it's not null before you try to access any of its properties.在尝试访问它的任何属性之前,只需测试它不是 null。
Try checking for comboBox.SelectedItem.ToString
instead of comboBox.SelectedValue.ToString
尝试检查comboBox.SelectedItem.ToString
而不是comboBox.SelectedValue.ToString
Hope that helps.希望有帮助。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.