RobertRFreeman
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.UserC ontrol
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.DataBindi ngs.Add(Ne w System.Windows.Forms.Bindi ng("BindMe ", Me.DataSet11, "Customers.CompanyName"))
'Save:
Me.BindingContext.Item(Me. DataSet11, "Customers").EndCurrentEdi t()
MsgBox("Has Changes: " & Me.DataSet11.HasChanges.To String)
Me.SqlDataAdapter1.Update( Me.DataSet 11)
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.UserC
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.DataBindi
'Save:
Me.BindingContext.Item(Me.
MsgBox("Has Changes: " & Me.DataSet11.HasChanges.To
Me.SqlDataAdapter1.Update(
http://www.freeweb.hu/noiseehc/SimpleBind.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 PropertyChangedEventHandle r
<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
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 PropertyChangedEventHandle
<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("
End If
End Set
End Property
you are welcome.
ASKER
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
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 PropertyChangedEventHandle r :-)
ASKER
There is. :) It just doesn't work like I thought. Let me know if you figure out what it does.
ASKER
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.Paint EventArgs)
MyBase.OnPaint(e)
'Gets rid of the selection
If Me.ComboBox1.SelectionLeng th > 0 Then
Me.ComboBox1.SelectionLeng th = 0
End If
End Sub
I may post this as another question.
Here is the workaround code in my databound combobox usercontrol:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.Paint
MyBase.OnPaint(e)
'Gets rid of the selection
If Me.ComboBox1.SelectionLeng
Me.ComboBox1.SelectionLeng
End If
End Sub
I may post this as another question.