views:

1777

answers:

2

I have a business object, which is a composite of child objects.
I am using databinding in Visual Studio 2008 to bind to controls on a Windows form.

But I am getting the above error in the InitializeComponent method of the form.

Lets say I have an object called ParentObject which contains a generic list, ChildListObject. The ParentObject also has Child object, which itself has a Child object. (ie ParentObject.ChildObject.ChildObject)

I set the main binding source:

BindingSource.Datasource = ParentObject

I add a grid and set it's binding source:

GridBindingSource.Datasource = ParentObject

and set the DataMember to:

BindingSourceB.DataMember = "ChildListObject"

Now, the grid's datasource is set to GridBindingSource:

Me.MyDataGridView.DataSource = Me.GridBindingSource

There are also other controls that are bound to properties of the ParentObject and the ParentObject.ChildObject

I have tested this in an isolated project and it works fine, so I am having trouble finding out what the real bug is? Code that used to work, will all of the sudden stop working.

The error I get is (if I use the names of the objects in the above example):

"DataMember property ChildObject cannot be found on the DataSource"

It fails on:

Me.MyDataGridView.DataSource = Me.GridBindingSource

Strangely, if I remove <System.Diagnostics.DebuggerStepThrough()> and then when it fails just continue it is fine??? But it still fails in runtime.

Does anyone have any ideas that could point me in the right direction? The closest I have found through googling is it may have something to do with the order of the generated designer code getting messed up. Code that was working, will all of the sudden stop working.


This issue seems to come and go. If I just continue after the error is raised the program happily continues with no problems. Possibly a bug in VS. But at run-time the error still exists.

What is causing this problem? How do I stop it occurring?

A: 

Datamember should be a string that defines what property of the List you want to show. Not necessary here.

This would make sense:

BindingSourceB.Datasource = ParentObject.ChildList;

If your binding to a grid, you don't set Datamember. Just set Datasource and then use the designer to configure the grid.

The GetType is there to help the designer. In designer properties you should set the BindingSourceB to ChildListObject type, and then do what I did above.

gcores
sorry the example was slighty wrong. I have edited and fixed. There actually was no in between object, just a parent object which contains as generic list of child objects.I wasn't setting the datamember property on the grid. But thanks for your help anyway.
ptutt
OK, I have edited my answer. Hope it's useful.
gcores
A: 

The only workaround I have found to do the following:

  1. Remove all columns from the grid
  2. Add the desired columns back to the grid (this prevents the columns from being deleted in the next step)
  3. Remove the grid datasource
  4. Set the grid's datasource in the form load event.
ptutt