views:

344

answers:

0

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?

Thanks