DataGridView.BeginEdit () завершается ошибкой, поскольку DataGridViewTextBoxEditingControl расположен - PullRequest
0 голосов
/ 13 сентября 2018

В приведенном ниже коде я просто пытаюсь сделать BeginEdit(False) в DataGridView.Иногда происходит сбой с:

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()
(...)

Я гарантировал, что ячейка является текущей ячейкой, то есть выбрана ... что еще я могуделать?Если я добавил это в тщетной попытке обойти проблему, то после отладки я вижу, что EditingControl в этот момент имеет значение Nothing, поэтому приведенный ниже код даже не работает на .EditingControl.CreateControl() (объект не создан).

Это можно легко скопировать: отредактируйте строку, затем выполните Escape.Escape удаляет DataGridView строку, в результате чего EditingControl становится Nothing.Любая дальнейшая попытка сделать BeginEdit приведет к указанной выше ошибке.

Как можно создать объявление EditingControl, связанное с ячейкой?

Сводка кода:

    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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...