all groups > dotnet windows forms databinding > july 2004 >
You're in the

dotnet windows forms databinding

group:

Synchronizing values


Synchronizing values Gabriel Michaud
7/8/2004 10:16:27 PM
dotnet windows forms databinding:
Hello,

I'm currently reading on .NET data binding and I'm trying to do one of the
sample programs in the book. There is a text box bound to a property of an
array, and a previous/next button. The book mentions that if there are many
controls bound to the same element I do not have to take care of anything
and the values will be synchronized automatically. It does work, however I
need to go to the next row and then go back to see my change applied on all
the text boxes.

I also tried changing the array in code to see if the form would be updated
to reflect my changes, but it is not updated.

Finally, I also tried hooking the CurrencyManager class to see if I could
get the ItemChanged() event, but I'm only getting the CurrentChanged()
event.

I saw some post regarding the need to have a <PropertyName>Changed event,
but this did not help. Here's part of my code. Am I doing anything wrong? Is
this the expected behaviour? If so, how can I force the databinding to
resynchronize? And finally, when would the ItemChanged event fire?

Thanks!

Public Class TrainingForm
Inherits System.Windows.Forms.Form

Private m_Candidates() As Candidate = _
{New Candidate("Bill Gates", "305"), _
New Candidate("Steve Ballmer", "320")}

Private WithEvents m_CurrencyManager As CurrencyManager

Private Sub TrainingForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CandidateName.DataBindings.Add("Text", m_Candidates,
"CandidateName")
ExamNumber.DataBindings.Add("Text", m_Candidates, "ExamNumber")
TextBox1.DataBindings.Add("Text", m_Candidates, "ExamNumber")
Label3.DataBindings.Add("Text", m_Candidates, "ExamNumber")

m_CurrencyManager = DirectCast(Me.BindingContext.Item(m_Candidates),
CurrencyManager)
End Sub

Private Sub NextButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles NextButton.Click
Me.BindingContext(m_Candidates).Position += 1
End Sub

Private Sub PreviousButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PreviousButton.Click
Me.BindingContext(m_Candidates).Position -= 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
m_Candidates(0).ExamNumber = "123"
End Sub

Private Sub m_CurrencyManager_ItemChanged(ByVal sender As Object, ByVal
e As System.Windows.Forms.ItemChangedEventArgs) Handles
m_CurrencyManager.ItemChanged
MsgBox("Item Changed")
End Sub

Private Sub m_CurrencyManager_CurrentChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles m_CurrencyManager.CurrentChanged
MsgBox("Current changed")
End Sub
End Class

Public Class Candidate
Private m_ExamNumber As String
Private m_CandidateName As String

Public Event ExamNumberChanged As EventHandler
Public Event CandidateNameChanged As EventHandler

Public Property ExamNumber() As String
Get
Return m_ExamNumber
End Get
Set(ByVal Value As String)
m_ExamNumber = Value
RaiseEvent ExamNumberChanged(Me, New EventArgs)
End Set
End Property

Public Property CandidateName() As String
Get
Return m_CandidateName
End Get
Set(ByVal Value As String)
m_CandidateName = Value
RaiseEvent CandidateNameChanged(Me, New EventArgs)
End Set
End Property

Public Sub New(ByVal candidateName As String, Optional ByVal examNumber
As String = "")
m_CandidateName = candidateName
m_ExamNumber = examNumber
End Sub
End Class

Re: Synchronizing values Gabriel Michaud
7/9/2004 7:19:32 AM
This answers my question. Thank you very much for the quick answer.

Gabriel

[quoted text, click to view]

RE: Synchronizing values v-yiy NO[at]SPAM online.microsoft.com (
7/9/2004 8:46:23 AM
Hi Gabriel,

when you edit text in a TextBox, the TextBox will enter "edit" mode, it
will not update the underlying datasource under you move to another
position in Currencymanager or call EndCurrentEdit method to leave the
"edit" mode explicitly.

Also, if you want the UI reflect the changes when you update the
datasource by code, you need implement a collection and implement
IBindingList interface to notify the databinding mechanism. You may take a
look at the doc of IBindingList interface , there is an sample code in MSDN
library.

If you just want to update the UI after changing datasource by code, you
may call Binding.SuspendBinding and then call Binding.ResumeChange to
force an update.

Does this answer your question?
If not, please feel free to let me know your question, maybe I can try to
explain in some other way.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
Re: Synchronizing values nguyencaoky
11/21/2005 12:00:00 AM

Could you give me your sample code that can reflect data source'
changes to binded control? I tried to follow tips o
http://blogs.msdn.com/dchandnani/ but it cannot work.

Thanks,
K

--
nguyencaok
-----------------------------------------------------------------------
nguyencaoky's Profile: http://www.hightechtalks.com/m31
View this thread: http://www.hightechtalks.com/t32306
AddThis Social Bookmark Button