Nickname
Nickname

Reputation: 1

New rows added to a DataGridView are not bound properly if its (BindingList)DataSource starts out empty

I have a DataGridView (DGV) to which I have bound a BindingList (BL). The object type contained within BL properly implements INotifyPropertyChanged.

If BL contains elements at the time it is bound to DGV, it works as expected; changes to the BL are reflected in DGV, and changes to rows in DGV are reflected in BL, including newly added rows.

My issue occurs if BL is empty at the time it is bound to DGV. In this case, programmatically adding a new row to BL will create a row in DGV as expected, but edits to that row are not captured in the associated objects in BL; rather than having the values shown in DGV, the new object's fields have their default values. Likewise, if the object is programmatically created with non-default initial values before being added to BL, those values are not shown in DGV.

Is there an explanation for this issue?

Rebinding BL to the DGV after any elements have already been added resolves the issue, with rows created both before and after the rebinding properly updating their bound items. I'd rather not have to rely on this behavior as a workaround without understanding the root cause.

EDIT: Inspecting one of the new rows in DGV shows that the DataBoundItem properly points to the object in BL, despite the contents not matching.

Upvotes: -1

Views: 23

Answers (0)

Related Questions