Manual data binding using WriteValue

If I turn off automatic updating of the binding data source by setting DataSourceUpdateMode = Never, and then use the button to update the entire batch (using binding.WriteValue), the problem arises - Namely, only the first data source with controlled control is updated. All other reset controls return to their original values.

This is because when the current object changes (as happens after the aforementioned WriteValue), if ControlUpdateMode = OnPropertyChange, then all other controls are reread to the value from the data source.

What is the standard way to avoid this problem?

One way is to get the class from BindingSource and add the WriteAllValues ​​method. This method does the following:

(1) For each binding save ControlUpdateMode

(2) For each binding, set ControlUpdateMode = Never

(3) For each binding, call the WriteValue method

(4) For each binding reset ControlUpdateMode to the stored value

(5) For each binding, if ControlUpdateMode = OnPropertyChange, call the ReadValue method.

Can you see any problems with this?

If you work with your own classes, will the IEditableObject implementation solve the problem?

In the other control I'm working on, I implement my own binding. The way I am facing the problem is the following code. (I set a minimum, I hope you can follow it!):

Private Shared ControlDoingExplicitUpdate As MyCustomControl = Nothing Private Sub UpdateDataSourceFromControl(ByVal item As Object, ByVal propertyName As String, ByVal value As Object) Dim p As PropertyDescriptor = Me.props(propertyName) Try ControlDoingExplicitUpdate = Me p.SetValue(item, value) Catch ex As Exception Throw Finally ControlDoingExplicitUpdate = Nothing End Try End Sub Private Sub DataBindingSource_CurrentItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) If (ControlDoingExplicitUpdate IsNot Nothing) AndAlso (ControlDoingExplicitUpdate IsNot Me) Then Exit Sub Me.UpdateControlFromDataSource() 'Uses ReadValue End Sub 

So, when UpdateDataSourceFromControl is called, all CurrentItemChanged events will be raised for all other controls in the same BindingSource. However, since ControlDoingExplicitUpdate is set, they will not be read into the value from the data source unless they become the control that made the update. ControlDoingExplicitUpdate is set to Nothing after all these events have completed, so normal maintenance resumes.

Hope you can follow this, and again, I ask, can you see any problems with this?

+7
data-binding winforms bindingsource
source share
2 answers

I had similar form requirements. In my case, I just wanted to bind data for all form controls when I clicked the Save Form button.

The best solution I've found is to set each DataSourceUpdateMode binding to OnValidation and then set the Property AutoValidate property to Disable. This prevents snapping when the focus changes between controls on the form. Then, in the Click event for my Save button, I manually check the form input and, if everything is ok, call the ValidateChildren form to trigger the binding.

This method also has the advantage of giving you complete control over how you validate input. WinForms do not include a good way to do this by default.

+4
source share

I believe I recently read about stackoverflow, where it was given as an answer: Disable two-way file binding

 public static class DataBindingUtils { public static void SuspendTwoWayBinding( BindingManagerBase bindingManager ) { if( bindingManager == null ) { throw new ArgumentNullException ("bindingManager"); } foreach( Binding b in bindingManager.Bindings ) { b.DataSourceUpdateMode = DataSourceUpdateMode.Never; } } public static void UpdateDataBoundObject( BindingManagerBase bindingManager ) { if( bindingManager == null ) { throw new ArgumentNullException ("bindingManager"); } foreach( Binding b in bindingManager.Bindings ) { b.WriteValue (); } } } 
+2
source share

All Articles