ObjectListView - ChildCollection

ObjectListView - ChildCollection

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


Crestline posted on Wednesday, October 11, 2006

Hi,

We have an root collection that contains a child collection.  When we drag the object onto the form to create a datagridview and binding source, then drag the child collection on to the form to create it's datagridview and binding source, we get an error for the child collection grid.  The way we hooked it up was just to load the root collection, create an ObjectListView of it and set the root collection binding source to the objectlistview just created. 

The error we get is:
"Unable to cast object of type 'Schedule.Library.Main.Department' to type 'Csla.ObjectView'."
Where Department is a child in the child collection which would be a row in the childs datagridview.

If we don't use the objectlistview and just set the root collection binding source to the root collection, everything works fine.  We need the ability to sort and filter which is obviously why we'd like to use the ObjectListView.

Any suggestions?

Thanks

Brian Criswell replied on Wednesday, October 11, 2006

I am not quite sure that I am following what you are doing.  Could you post some code?  Maybe of where you are loading the object and setting the bindingSource.DataSource?

Crestline replied on Wednesday, October 11, 2006

Form loading

The Units object has a child collection of Departments, and when using the ObjectListView, the datagrid hooked up to the Departments won't load giving the error stated above.  Does the ObjectListView support child collections and such?

Works...
        private Schedule.Library.Main.Units _units;
        private ObjectListView _unitsView;

        public UnitTesting()
        {
            InitializeComponent();
        }
        private void Units_Load(object sender, EventArgs e)
        {
            this._units = Schedule.Library.Main.Units.GetUnits(1);
            this.unitsBindingSource.DataSource = this._units;    
        }

Doesn't work
        private Schedule.Library.Main.Units _units;
        private ObjectListView _unitsView;

        public UnitTesting()
        {
            InitializeComponent();
        }
        private void Units_Load(object sender, EventArgs e)
        {
            this._units = Schedule.Library.Main.Units.GetUnits(1);
            _unitsView = new ObjectListView(this._units, "RunNum Asc");
            this.unitsBindingSource.DataSource = _unitsView;    
        }

Let me know if you need more info.

Thanks.

Brian Criswell replied on Wednesday, October 11, 2006

That is all pretty normal usage, so the problem is most likely elsewhere.  How does Department fit into this?

Crestline replied on Wednesday, October 11, 2006

Brian Criswell:
Okay, how does Department fit into this?


When we add the Units object as a datasource, you can see all of it's properties, one being the Departmetns collection.  When we drag the Units object onto the form it creates a datagridview and a bindingsource.  If I expand the Units datasource, and drag the Departments collection onto the form, it creates another datagrid and binding source.  If we simply load the Units object and set it as the datasource for the Units binding source (and that's it, you DON'T have to do anything else like set the datasource for the Departments binding source, it's taken care when we drug the Departments collection on to the Form), both the Units Grid and Departments grid poplulate with the corrected binding between the two.  If we set the Units binding source to that of an ObjectListView, the Departments grid is having issues.

Not sure if that's what you mean...

Brian Criswell replied on Wednesday, October 11, 2006

Okay, I will set up a mock up and see what I can find.

Brian Criswell replied on Wednesday, October 11, 2006

Well, it looks like my understanding of ITypedList and ICustomTypeDescriptor were stretched beyond their limits.  I will look into this and try to get a solution to you by Friday.

Crestline replied on Wednesday, October 11, 2006

Brian Criswell:
Well, it looks like my understanding of ITypedList and ICustomTypeDescriptor were stretched beyond their limits.  I will look into this and try to get a solution to you by Friday.


Thanks Brian, we really appreciate it.  The is a great addition to the CSLA framework!

Brian Criswell replied on Friday, October 13, 2006

I have not forgotten about you.  I have gotten the ObjectListView working in the designer, but you cannot set the values in the child list.  I will let you know when it is finished, probably Monday.

Brian Criswell replied on Saturday, October 14, 2006

Unfortunately I never found the ideal solution, and I could not even simulate what you wanted to do with a DataView and typed DataSet.  I can offer a good workaround however.
  1. Set up data sources for the parent list type and the child list type
  2. Drag the parent list data source onto your control to create a DataGridView
  3. Drag the child list data source (not the Children property on the parent list) onto your control to create another DataGridView
  4. Set the parent list binding source's data source to a new ObjectListView when load the control/pass the parent list into the control
  5. handle the parent list binding source's CurrentChanged event and place the following code in it

private void itemListBindingSource_CurrentChanged(object sender, EventArgs e)
{
    try
    {
        childItemListBindingSource.DataSource = null;

        if (itemListBindingSource.Current is Csla.ObjectView)
        {
            Csla.ObjectView  objectView = (Csla.ObjectView)itemListBindingSource.Current;
            if (objectView.Object != null)
            {
                childItemListBindingSource.DataSource = new Csla.ObjectListView(((MyParentObject)objectView.Object).Children);
            }
        }

        childItemListBindingSource.AllowNew = childItemListBindingSource.DataSource != null;
    }
    catch (Exception ex)
    {
       // handle exception
    }
}

This should work with the version that you have, but I have posted a new version with some additional capabilities.

Crestline replied on Saturday, October 14, 2006

Thanks Brian, I'll give this a try.  Looks very straight forward.

Copyright (c) Marimer LLC