我花了一天时间试图弄清楚如何解决我的问题,这与
相同 this related unanswered question
在第一个组合框中,我的代码工作正常,没问题。当我尝试更改组合框时,会抛出错误Null Reference Exception
这是我的代码:
Private Sub dgvSurveyQuestions_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvSurveyQuestions.EditingControlShowing
Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
If Not editingComboBox Is Nothing Then
'Add the handle to your IndexChanged Event
RemoveHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
End If
'Prevent this event from firing twice, as is normally the case.
'RemoveHandler dgvSurveyQuestions.EditingControlShowing, AddressOf dgvSurveyQuestions_EditingControlShowing
End Sub
Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim editingComboBox As ComboBox = TryCast(sender, ComboBox)
If editingComboBox Is Nothing Then Exit Sub
'Show your Message Boxes
MessageBox.Show(editingComboBox.SelectedValue.ToString) ' throws error here
End Sub
我还在处理不同的解决方法,例如在填充datagridview时添加处理程序或者其他什么..我甚至不知道这是否可行,但我需要这样做。
我真的被困在这里,有人可以解释一下,并建议我该怎么办?感谢
答案 0 :(得分:2)
只需检查editingComboBox.SelectedValue
是否 Nothing 。
DataGridView
仅重用一个ComboBox
实例,并在用户选择另一个DataSource
单元格时在内部重置ComboBox
的{{1}}。
然后,事件ComboBox
加注,SelectedIndexChanged
当时将 Nothing 。