0

I have a datagridview displaying user information. The datasource is a dataset table, and I want the values of this dataset to change dynamically when the use edits the values in the datagridview. To do this I have harnessed the datagridview's cellvaluechanged event.

Some of the fields require a combobox with a dropdown list or allowable fields. So in these cases I create a comboboxcell.

All of this works great. HOWEVER in a few cases I need a drop-down and I need to allow manual entry of the cell.

I found a solution right here on stack overflow that MOSTLY works:

how to allow user manual entry in datagridview combobox in c#

....by handling the datagridview.editingcontrolshowing event and allowing it to call the datagridcell.validating event, I can add the freeform text to the comboboxcell dropdown list and select it. My updated code is below.

All of the above works great, IF after entering a free form input, the user simply clicks on another cell. HOWEVER, if the user instead hits ENTER (like a user is likely to do), this triggers another cellvaluechanged event. Running through my code line-by-line, this still seem to work until the end of the validating event when I immediately get the following:

System.NullReferenceException: Object Reference not set to an instance of an object at System.Windows.Forms.DatagridView.EndEdit.....

This is an unhandled exception that does not break on any of my code, so I cannot see exactly where it is occurring. I am assuming that the combobox is disposing when the datasource has been updated by the cellvaluechanged event. But I do not know how to verify that, and more importantly how to resolve, it.

Bottomline, I need users to be able to hit "enter" in the datagridviewcombobox, with that blowing up.

Any suggestions would be appreciated.

    Private Sub propertiesGrid_CellValueChanged(sender As Object, e As System.EventArgs) Handles propertiesGrid.CellValueChanged

       CFS_DatasetMain.CFSPROPERTIES.AcceptChanges()
    End Sub

 Public Sub propertiesGrid_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles propertiesGrid.EditingControlShowing
        Dim cbo As DataGridViewComboBoxEditingControl

                If TypeOf (e.Control) Is DataGridViewComboBoxEditingControl Then
                    cbo = e.Control
                    cbo.DropDownStyle = ComboBoxStyle.DropDown
                    AddHandler cbo.Validating, AddressOf cbo_Validating
                End If
    End Sub


  Public Sub cbo_Validating(sender As Object, e As EventArgs)
        Dim cbo As DataGridViewComboBoxEditingControl
            cbo = CType(sender, DataGridViewComboBoxEditingControl)

            If (cbo.Items.IndexOf(cbo.Text) = -1) Then
                 cbo.Items.Add(cbo.Text)
                   propertiesGrid.CurrentCell.Value = cbo.Text
            End If
 End Sub
Ess Kay
  • 598
  • 4
  • 20
user1723280
  • 11
  • 1
  • 2
  • you can verify if combo is disposed by hovering over it when the error hits. If its set to `Nothing`, then its probably the issue – Ess Kay Oct 08 '19 at 18:57
  • Hi Ess Kay. Thank you for the suggestion. It looks like that is not the issue, the datagridcomboboxcell is still active after returning to the validating eventhandler from the cellvaluechanged event. But as soon as I my validating event handler ends (as a step past "End Sub"), is when I get the null reference. I have no idea what is coming up null. – user1723280 Oct 09 '19 at 02:25
  • There can be several issues. One (unlikely) could be something with a DLL you have. More likely its something else in your code, but without seeing the rest of your code we cannot help. – Ess Kay Oct 10 '19 at 14:03
  • https://stackoverflow.com/a/39657876/2267583 have a look over there see if it helps – Ess Kay Oct 10 '19 at 14:07

0 Answers0