Databinding: Child Collections/Child Bindingsources

Databinding: Child Collections/Child Bindingsources

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


Patrick.Roeper posted on Monday, October 22, 2007

This is a curiousity question geared towards databinding (and possibly CSLA)... ProjectTracker > PTWin > ProjectEdit.cs has an editable root project object with its 'resources' collection databound towards the bottom of the control.

There are 2 bindingsources on the control related to this Project -> Resources object hierarchy: projectBindingSource & resourcesBindingSource.

projectBindingsource has a datasource of 'ProjectTracker.Library.Project' and resourcesBindingSource is set to 'projectBindingSource' with a DataMember of 'Resources'.

I am unsure as to why 2 bindingsources are needed? The resources collection of a project is displayed in a DataGridView control with its datasource set to the resourcesBindingSource. If I were to changed the datasource of the DataGridView control to point to projectBindingSource and change its datamember property to be 'Resources' (basically moving the databinding off of a bindingsource control and putting it directly on the UI control) I get the same runtime result... I think?

Can someone explain what function is served by keeping the child collection's databinding in a seperate bindingsource? I was thinking it had something to do with BeginEdit()'s needing to be called on bindingsources directly or something.... Thanks in advance.

ajj3085 replied on Monday, October 22, 2007

Hmm, good question.

The only answer I can think of is if you want to listen for events from the collection but not the parent object.  You would wire your events in the designer for the BS_ItemChanged and BS_CurrentItemChanged and not need to worry about removing / adding the event handlers when the collection changes (such as when you get a clone back from remoting).

I'm not sure about the BeginEdits one way or the other... like I said, good question. Smile [:)]

RockfordLhotka replied on Thursday, November 01, 2007

This is the result if you do drag-and-drop from the Data Sources window to set up the bindings on the controls. I always use drag-and-drop binding to bind my controls, as I find that to be more reliable than doing the bindings by hand.

However, if setting the DataMember by hand works for you and makes your world simpler, then go for it! :)

Patrick.Roeper replied on Thursday, November 01, 2007

Well the value add is the SaveProject code can now be removed of any child bindingsource references, and using generics I can now have 1 method that will take care of all UI-Root Business object saving. I try to promote tight coupling when I am given the opportunity!

Copyright (c) Marimer LLC