Solved

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

Posted on 2004-09-16
8
495 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
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:jackiechen858
ID: 12086352
0
 
LVL 7

Accepted Solution

by:
jackiechen858 earned 500 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
 
LVL 7

Expert Comment

by:jackiechen858
ID: 12087885
you are welcome.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IO.FileSystemWatcher dies when used in Powershell 1 45
XML & .net 5 41
Export import database 4 43
Where can I ask about my ASP.NET MVC project? 6 23
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now