Rebinding BO + _PropertyChange event miss firing

Rebinding BO + _PropertyChange event miss firing

Old forum URL: forums.lhotka.net/forums/t/6788.aspx


jamie.clayton posted on Tuesday, April 14, 2009

Guys I'm stuck trying to debug a databinding behaviour in windows forms. I've got a parent CSLA 3.0.3 BO with a child BO (VB code).  I've created a screen and used created a bindingsource that uses the  private variable (withevents) in the forms code. When editing the child BO I open a form and pass a copy of the Parent BO to it and allow the child form to edit the BO, then when I return back to the parent form, rebind all the datasources with the copy from the child form.  It's a fairly complicated parent/child BO and there are lots of BO_PropertyChange events at the business object and screen level to handle information changing.

If the child object is edited, everything works as expected. If I create a new child object, then when I return to the parent screen I loose the parent BO_PropertyChange event (it just seems to disconnect).  I don't understand this behaviour because the BO_Property Let/Get fires, Business Rules fire and the Parent forms Private variable mBO_PropertyChange event also fires.

Any suggestion on what to do.  I've spent 5 hours trying different re-binding code approaches with little luck.

Here is some sample code (cut down a lot)

This is the private variable

Private WithEvents mSale As Sale = Nothing


This is the load mechanism for the parent screen.

Private Sub Sales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.Cursor = Cursors.WaitCursor

If Not Me.DesignMode Then

SaleBindingSource.DataSource = mSale

If mSale.SaleFKClientID <> 0 AndAlso Not IsNothing(mSale.Client) Then

' Display Client name.

With ClientBindingSource

.DataSource = mSale.Client

.RaiseListChangedEvents = True

.ResetBindings(False)

End With

ApplyAuthorizationRules()

Me.Cursor = Cursors.Default

End Sub

This is the load mechanism for the Child screen.

Private Sub OpenClientScreen(ByVal clientID As Integer)

Me.Cursor = Windows.Forms.Cursors.WaitCursor

Dim tempCopy As Sale

tempCopy = mSale.Clone

Dim frm As New frmClientEntry(tempCopy)

Me.Cursor = Windows.Forms.Cursors.Default

With frm

.ShowDialog()

Select Case .DialogResult

Case DialogResult.Yes

' The user might have created a new client record (so we still need to treat is as a child object)

If Not .Client.IsChild Then

.Client.MarkAsChild()

End If

' Need to switch off the databinding for a little bit.

With SaleBindingSource

.RaiseListChangedEvents = False

.EndEdit()

.DataSource = Nothing

End With

With ClientBindingSource

.RaiseListChangedEvents = False

.EndEdit()

.DataSource = Nothing

End With

' Update the client from the clients screen copy of the sales BO.

' This is important because its painful to change child objects on a popup form.

mSale = .Sale.Clone

DataBindingHelper.Rebind(SaleBindingSource, mSale)

DataBindingHelper.Rebind(ClientBindingSource, mSale.Client)

ApplyAuthorizationRules()

Case Else ' Cancel.

' Ignore the changes.

End Select

' If the user deletes the client record (god forbid and it's not used else were,

' then the sale record should clear that client reference

If .DeletedClientId = clientID Then

mSale.SaleFKClientID = 0

' This should then set the clientNameTextBox.text = String.Empty

With ClientBindingSource

.DataSource = Nothing

.RaiseListChangedEvents = True

.ResetBindings(False)

End With

End If

End With

End Sub

The parent object, creates a new child using the following 

Public Sub NewClient(ByVal markAsChild As Boolean)

'Call a method which calls DataPortal.Create(Of Client)

msaleClient = Client.NewClient()

' Ok so the sale link to client is a new record (and that changes how we save these records)

msaleFKClientID = mClient.PKClientID

If markAsChild Then mClient.MarkAsChild()

End Sub

Copyright (c) Marimer LLC