Question

If I turn off automatic updating of a binding data source by setting DataSourceUpdateMode = Never and then use a button to update the whole lot (using binding.WriteValue), a problem occurs - Namely, only the first bound control's data source is updated. All other controls are reset back to the original values.

This is because when the current object changes (as happens after the above WriteValue), if ControlUpdateMode = OnPropertyChange, then all the other controls re-read in the value from the data source.

What is the standard way of avoiding this issue?

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

(1) For each Binding, save the ControlUpdateMode

(2) For each Binding, set ControlUpdateMode = Never

(3) For each Binding, call the WriteValue Method

(4) For each Binding, reset ControlUpdateMode to the saved value

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

Can you see any issues with doing this?

If working with your own classes, would implementing IEditableObject resolve the issue?

In another control I'm working on, I implement my own binding. The way I get around the issue in that is with the following code. (I've put in the bare 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 the CurrentItemChanged events will be called for all other controls in the same BindingSource. However, because ControlDoingExplicitUpdate is set, they will not re-read in the value from the data source unless they happen to be the control that did the updating. ControlDoingExplicitUpdate is set to Nothing after all these events have completed, so that normal service resumes.

I hope you can follow this, and - again - I ask, can you see any issues with this?

Was it helpful?

Solution

I have had similar requirements for a form. In my case I only wanted the databinding for all the form's controls to occur when I clicked the form's Save button.

The best solution I found was to set each binding's DataSourceUpdateMode to OnValidation then set the containing form's AutoValidate property to Disable. This prevents binding as you change focus between controls on the form. Then in the Click event for my Save button, I manually validate my form's input and, if it is OK, call the form's ValidateChildren method to trigger the binding.

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

OTHER TIPS

I believe I recently read on stackoverflow where this was given as an answer: Disable Two Way Databinding

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 ();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top