Solved

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

Posted on 2004-09-16
8
492 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

8 Experts available now in Live!

Get 1:1 Help Now