DataGridView Object Databinding Issue “Index -1 does not have a value”

后端 未结 4 1254
借酒劲吻你
借酒劲吻你 2020-12-21 04:41

I am using a set of DataGridViews (dgv) to display a class\'s members via properties. I use the dgv so that the user can edit the value of the member right in the view (so

4条回答
  •  北海茫月
    2020-12-21 04:47

    I know this question is old but:

    If you initially bind an empty collection that does not inform the DGV of changes (e.g. a Collection does not, but a BindingList does), the initial current row offset will be correctly set to -1, (Because it is empty.)

    When you subsequently add objects to your data bound collection they will still display correctly on the grid, but the CurrencyManager will not be informed of any changes, and the current row offset will remain stubbornly at -1.

    So, when you try to edit a row, the CurrencyManager thinks you are trying to edit a row at offset -1, and the exception is thrown.

    To combat this, you need to rebind before interacting with a row, or initially bind a Collection etc when it contains one or more items.

提交回复
热议问题