?
Solved

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

Posted on 2004-09-16
8
Medium Priority
?
507 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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