DataBinding, BindingContext and BindingManagers
I have spent the past 2 days trying to figure all of this out. Our app DataBinds to many data sources, all ADO.NET types. Mostly from one dataset, but in some cases, like our drop downs that load from a code table, they are not related.
So, to do an Update or an Insert, one needs to call EndCurrentEdit on the BindingManager. This propagates the changes to the DataSet. Well, when there are numerous BindingManagers it would be nice to loop through all of them and call EndCurrentEdit on each. Easy enough, Forms BindingContext Implements IEnumerable and Collection. Hard enough though, as it's Hidden, or not meant to be used from code.
So, a dirty work around is this. When the user clicks Save, in our case this can be for an Insert or an Update, we do this:
Dim de As DictionaryEntry
For Each de In Me.BindingContext
Dim we As WeakReference
we = de.Value
If we.IsAlive Then
Dim cm As CurrencyManager
cm = we.Target
cm.EndCurrentEdit()
End If
Next
This will propagate user modifications to the DataSource, which, in our case is a DataSet. We push the DataSet changes across to a Data Tier and life is good.
I initially tried setting up this loop with a BindingManagerBase type, then a CurrencyManager type, both of which threw an InvalidCastException. Anyone know why one cannot loop through BindingContext with a BindingManagerBase Type? The above works, but sure seems like a hack to me.