user1723280
user1723280

Reputation: 11

Adding values to datagridcomboboxcell during validation + hitting Enter = Null Reference

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

Upvotes: 0

Views: 80

Answers (0)

Related Questions