In the code below, I simply try to do a BeginEdit(False)
in a DataGridView. Occasionally, this fails with:
System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object.
Object name: 'DataGridViewTextBoxEditingControl'.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
(...)
I've ensured that the cell is the current cell, that is is selected... what else can I do? With the If that I added in a vain attempt to get around the issue, I see, by debugging, that the EditingControl is Nothing
at that point, so the code below even fails on .EditingControl.CreateControl()
(Object not instantiated).
It can be replicated easily: edit a line, then do Escape. Escape has the DataGridView
delete the line, resulting in the EditingControl
becoming Nothing
. Any further attempt to do a BeginEdit
will result in the above error.
How can an EditingControl
be instantiated ad associated to a cell?
A summary of the code is:
dgv.Focus()
dgv.CurrentCell = dgv.Rows(intRowNr).Cells(intColumnNr)
dgv.CurrentRow.Cells(intColumnNr).Selected = True
dgv.Refresh()
If dgv.EditingControl = Nothing OrElse dgv.EditingControl.IsDisposed Then
' Everything here in the If is a vain attempt to circumvent the problem.
dgv.EditingControl.CreateControl() ' Fails because the EditingControl is sometimes Nothing
dgv.CurrentCell.InitializeEditingControl(intRow, dgv.Rows(intRow).Cells(0).Value, dgv.DefaultCellStyle)
End If
dgv.BeginEdit(False) ' If the If above is not there,
' this occasionally fails with the error above.
User contributions licensed under CC BY-SA 3.0