Link to home
Start Free TrialLog in
Avatar of RobertRFreeman
RobertRFreemanFlag for United States of America

asked on

Difficult! Bound user control property haschanges in dataset when no changes are made.

When databinding to the text property of a textbox the getchanges property of the dataset will only return values if the textbox is changed.
If I bind to a property of any usercontrol, dataset.haschanges will always be true.
I want my property to work like the text property.
Any code replies can be in C#.NET or VB.NET.

Here is the relevent code:

'User Control Property Declaration:
_________________________________________________________
Public Class UserControl1
    Inherits System.Windows.Forms.UserControl
    Private _BindMe As String

    <Bindable(True)> _
    Public Property BindMe() As String
        Get
            Dim myBinding As Windows.Forms.Binding
            Return _BindMe
        End Get
        Set(ByVal Value As String)
            _BindMe = Value
        End Set
    End Property

End Class
__________________________________________________________
'FORM Code

'DataBinding:
Me.UserControl11.DataBindings.Add(New System.Windows.Forms.Binding("BindMe", Me.DataSet11, "Customers.CompanyName"))

'Save:
Me.BindingContext.Item(Me.DataSet11, "Customers").EndCurrentEdit()
MsgBox("Has Changes: " & Me.DataSet11.HasChanges.ToString)
Me.SqlDataAdapter1.Update(Me.DataSet11)
Avatar of jackiechen858
jackiechen858
Flag of Canada image

ASKER CERTIFIED SOLUTION
Avatar of jackiechen858
jackiechen858
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobertRFreeman

ASKER

Thank you both very much.  Here is the code in VB.  Now I can make properties that handle simple binding correctly.  WooHoo!
I found more documentation under Property Usage Guidelines in MSDN by searching for "property-changed events" or "properties, usage guidelines" in the index.

    Public Event BindMeChanged As PropertyChangedEventHandler

    <Bindable(True)> _
    Public Property BindMe() As String
        Get
            Dim myBinding As Windows.Forms.Binding
            Return _BindMe
        End Get
        Set(ByVal Value As String)
            If _BindMe <> Value Then
                _BindMe = Value
                RaiseEvent BindMeChanged(Me, New PropertyChangedEventArgs("BindMe"))
            End If
        End Set
    End Property
you are welcome.
My last post has a bug, here is the correct code.

    Private _BindMe As String
    Public Event BindMeChanged As EventHandler

    <Bindable(True)> _
    Public Property BindMe() As String
        Get
            Dim myBinding As Windows.Forms.Binding
            Return _BindMe
        End Get
        Set(ByVal Value As String)
            If _BindMe <> Value Then
                _BindMe = Value
                RaiseBindMeChanged()
            End If
        End Set
    End Property

    Protected Sub RaiseBindMeChanged()
        RaiseEvent BindMeChanged(Me, New EventArgs)
    End Sub
When I read your code, I really thought there is a PropertyChangedEventHandler :-)
There is. :)  It just doesn't work like I thought.  Let me know if you figure out what it does.
One other issue I found with using a usercontrol as a combobox is that the text is highlighted on initialization.  I think I found a workaround, but this solution causes an error in the design time environment.  When the control is on another form, the following VS.net error occurs: '-195465169' is not a valid value for 'start'.

Here is the workaround code in my databound combobox usercontrol:
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        'Gets rid of the selection
        If Me.ComboBox1.SelectionLength > 0 Then
            Me.ComboBox1.SelectionLength = 0
        End If
    End Sub

I may post this as another question.