title: Get DataGridViewComboboxColumn SelectedValue (VB.Net)
body: <p>I need to get the selected value of a ComboBox in a DataGridView. I have it partially working, but I get a <em>Null Reference Exception</em> if I change a another ComboBox in the grid. Here's my code:</p>
  
  <pre><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: " &amp; comboBox.SelectedValue.ToString)
  End Sub
  </code></pre>
  
  <p>This works fine the first time the ComboBox is changed, but generates a Null Reference Exception if another ComboBox is changed. Any ideas why this is happening? Note: I found most this code on MSDN's discussion forms.</p>
  
  <p>Thanks!</p>
  
  <p>Peter</p>