我需要在DataGridView中獲取ComboBox的選定值。我有它的部分工作,但我得到一個空引用異常如果我更改網格中的另一個組合框。這裏是我的代碼:Get DataGridViewComboboxColumn SelectedValue(VB.Net)
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的討論表單中發現了大部分代碼。
謝謝!
彼得