Issue binding datagrid with BindingSourceHelper/BindingSourceNode

Issue binding datagrid with BindingSourceHelper/BindingSourceNode

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


sroberts posted on Monday, February 02, 2009

Previously to the new CSLA Binding Source Helper object I was using the Public Sub UnbindBindingSource(ByVal source As BindingSource, ByVal apply As Boolean, ByVal isRoot As Boolean) way of doing unbinding prior to a save/cancel etc.

I decided to switch to using the new helper objects to generally make the code easier (and to embrace the change) but my form has now lost binding on a DataGridView and I can't work out why.

Let me explain...

I have a parent Quotation class (BusinessBase) that contains a managed property QuotationItems (BusinessListBase) this child class in turn is a collection of QuotationItem (BusinessBase)class objects. I use lazy loading of the QuotationItems property, so far so good...

I have a Windows Form where the text/combobox controls are bound to the Quotation class and a DataGridView which is bound to the QuotationItems class, both via a standard BindingSource control.

In code I use the CSLA BindingSourceNode and BindingSourceHelper objects to do my binding and Save, Cancel functionality.

When the form loads, the text/combobox fields bind to the Quotation object just fine, however the datagrid is blank. I've put a break point on the Child_Fetch of the child object and can see that being called and the object being populated.

So I'm not sure if I've missed something I have to do with the BindingSourceNode/Helper concerning the loading/binding of child objects. The CSLA binding source helper objects must be working to some extent as I have binding of the parent.

Below, I've posted some simplified code of what I'm doing behind the scenes.

(mQuotation is a local object reference that gets initialised during the form's New creator)

Private WithEvents mQuotation As Quotation = Nothing
Private mCSLA_BindingSourceNode As Csla.Windows.BindingSourceNode

Public Sub New(QuotationID As Integer)

   mQuotation = Quotation.GetQuotation(QuotationID)
   BindUI()

End Sub

Private Sub BindUI()

   mCSLA_BindingSourceNode = Csla.Windows.BindingSourceHelper.InitializeBindingSourceTree(Me.components, bsQuotation)
   mCSLA_BindingSourceNode.Bind(mQuotation)

End Sub


public sub cmdSave () Handles cmdSave.Click

   mCSLA_BindingSourceNode.Apply()
   mQuotation = mQuotation.Save
   BindUI()

End Sub


Any help would be much appreciated.

sroberts replied on Monday, February 02, 2009

I just tried revising my BindUI() code to the following;

Private Sub BindUI()

   mCSLA_BindingSourceNode = Csla.Windows.BindingSourceHelper.InitializeBindingSourceTree(Me.components, bsQuotation)
   mCSLA_BindingSourceNode.Bind(mQuotation)
   mCSLA_BindingSourceNode.Bind(mQuotation.QuotationItems)

End Sub

In the hope that my error is due to not explicitly asking the helper object to bind the child collection, but this just produces a runtime error 'BeginEdit is not valid on a child object'

sroberts replied on Tuesday, February 03, 2009

Does anyone use the CSLA BindingSourceHelper / BindingSourceNode objects ?

It's looking like I have to go back to the long winded way of de-coupling my UI from the objects prior to a save/cancel operation as I just can't get my child object binding on the DataGridView using the above.

lukky replied on Tuesday, February 03, 2009

Hi,

Does the "child" BindingSource point to the "parent" BindingSource ?

That is, bsQuotationItems.DataSource must be bsQuotation, and bsQuotationItems.DataMember must be the child property of the parent, in your case "QuotationItems". You should be able to set this at design time assuming both BindingSource are created at design time.

Hope this helps.

sroberts replied on Tuesday, February 03, 2009

Luc,

That solved the problem, thank you very much for your post.

bsQuotationItems.DataSource was referencing the base child class in the my object hierachy (probably from when I initially created the binding source and linked it to the class containing the data for it) and I had never seen a reason to set the DataMember property so this was blank.

As soon as I set the DataSource on bsQuotationItems to point at bsQuotation, I then had a dropdown available on DataMember that of course contained a single choice of QuotationItems (this being the public property Getter on the parent class)


I'm so happy that this now works and my code can use the CSLA BindingSourceHelper and BindingSourceNode helper objects as these remove the need to use and manual edit state handling and de-coupling code to detach form based binding sources from their business objects.

Also hopefully this post will help others in a similar situation.

Copyright (c) Marimer LLC