0

I've spent a day trying to figure out how to solve my problem, which is identical to

this related unanswered question

On the first combobox, my code works fine, no problem. When I try to change the combobox it throws an error Null Reference Exception

This is my code:

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

I'm still working on a different workaround, like adding a handler while the datagridview is populated or whatever.. I don't even know if this is possible but I need to do this.
I'm really stuck here, can someone shed some light and advice me on what to do? Thanks

Community
  • 1
  • 1
Codemunkeee
  • 1,585
  • 5
  • 17
  • 29
  • Could you debug and tell us what is exactly the `TryCast` returning to the variable `editingComboBox`? – Matteo NNZ Nov 18 '14 at 10:14
  • @MatteoNNZ have you any speculation? TryCast returns a reference (which is irrelevant what type, as long as the error is `Null Reference Exception`) or Nothing (Then the line in which the error occurred, should not be performed - `If editingComboBox Is Nothing Then Exit Sub`). – dovid Nov 18 '14 at 10:47
  • 1
    @lomed no speculation, the ´Nothing´ case of the ´Trycast´ is managed already so i feel like the problem is that ´combobox.selectedValue ´ is nothing, that's why i was asking to debug. I don't see any other reason to throw a null exception there – Matteo NNZ Nov 18 '14 at 11:42
  • @MatteoNNZ you right. – dovid Nov 18 '14 at 14:59

1 Answers1

2

Simply check if editingComboBox.SelectedValue is Nothing.

The DataGridView reuses only one ComboBox instance, and internally resets the DataSource of the ComboBox when the user selects another ComboBox cell.
Then the event SelectedIndexChanged raises and SelectedValue will be Nothing at that time.

dovid
  • 6,354
  • 3
  • 33
  • 73
Ripple
  • 1,257
  • 1
  • 9
  • 15
  • 1
    @lomed, please read well the answer, Ripple is writing "selectedValue" which is a property that can return Nothing in case of Non-DataBinding. Moreover, it's really the only reason why he's getting Null Exception in that part of code. For sake of honesty remove the downvote. – Matteo NNZ Nov 18 '14 at 13:33
  • Great @lomed. I really believe this must be the problem, let's wait for asker feedback – Matteo NNZ Nov 18 '14 at 17:27