获取DataGridViewComboboxColumn SelectedValue(VB.Net)
||
我需要在DataGridView中获取ComboBox的选定值。我有部分工作,但如果更改网格中的另一个ComboBox,则会收到Null引用异常。这是我的代码:
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 Reference Exception。任何想法为什么会这样?注意:我在MSDN的讨论表上找到了大多数此代码。
谢谢!
彼得
没有找到相关结果
已邀请:
3 个回复
味芯憨
属性之前测试comboBox是否为空:
在我看来,当将“ 1”从旧值设置为新值时,新旧组合框都会调用此SelectedIndexChanged事件。我怀疑当调用旧ѭ1时,发件人为null / Nothing,因为其值正在更改。也许。但是无论发生什么,null都是null。在尝试访问其任何属性之前,只需测试它是否不为null。
闪脖
而不是
希望能有所帮助。
细瑞