Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-16
8
Medium Priority
?
509 Views
Last Modified: 2008-03-10
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)
0
Comment
Question by:RobertRFreeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:jackiechen858
ID: 12086352
0
 
LVL 7

Accepted Solution

by:
jackiechen858 earned 2000 total points
ID: 12086643
           public string BindMe
            {
                  get
                  {
                        return _BindMe;
                  }
                  set
                  {
                        if ( _BindMe != value)
                        {
                              _BindMe = value;
                              RaiseChangeEvent();
                        }
                        else
                        {
                        }
                  }
            }
            public event EventHandler BindMeChanged ;
            private void RaiseChangeEvent ()
            {
                  BindMeChanged(this,null);
            }

0
 
LVL 16

Author Comment

by:RobertRFreeman
ID: 12086997
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
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 7

Expert Comment

by:jackiechen858
ID: 12087885
you are welcome.
0
 
LVL 16

Author Comment

by:RobertRFreeman
ID: 12088652
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
0
 
LVL 7

Expert Comment

by:jackiechen858
ID: 12088700
When I read your code, I really thought there is a PropertyChangedEventHandler :-)
0
 
LVL 16

Author Comment

by:RobertRFreeman
ID: 12088741
There is. :)  It just doesn't work like I thought.  Let me know if you figure out what it does.
0
 
LVL 16

Author Comment

by:RobertRFreeman
ID: 12221367
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question