I have Companies
and Contracts
in a parent-child relationship, shown in two DataGridViews in a winforms app. They are both bound to collections of Entity models.
How can I add a new contract?
If I do this,
Dim c as Company = CompaniesBindingSource.Current c.contracts.Add(New Contract()) context.SaveChanges()
the grid doesn't refresh (even if I call .Refresh()
, or .ResetBindings()
on the BindingSource. I have to navigate away from the selected company and back to it, for the grid to refresh.
If I do ContractsBindingSource.AddNew()
, the grid refreshes, but the data doesn't persist to my entity context.
Note: my contracts collection is a property on my Company
model:
Public ReadOnly Property activeContracts As SortableBindingList(Of Contract)
Get
Dim list = New SortableBindingList(Of Contract)
For Each contract As Contract In contracts.Where(Function(c) c.isActive).ToList
list.Add(contract)
Next
Return list
End Get
End Property
Does this have anything to do with it? How can I force a property to recompute?
This worked for me:
CompaniesBindingSource.ResetCurrentItem()
This recalculated the Contracts
property, and automatically refreshed the contracts grid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.