DataMember property [ObjectName] cannot be found on the DataSource
Asked Answered
B

6

6

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?

Bovid answered 26/2, 2009 at 1:29 Comment(0)
B
2

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.
Bovid answered 28/10, 2009 at 21:33 Comment(0)
M
4

I tried several experiments on this. The problem only happens in this condition if you have a BaseForm and BindingSource on it, if you inherit a new InheritedForm from this BaseForm, or
if you have additional binding source on InheritedForm related to the BindingSource (which inherited by BaseForm) you have the designer error. I don't have a designer solution but if you simply ignore and continue everything is going to be normal when you build project again or you will have to set the datamember by code.

Maliamalice answered 7/6, 2011 at 7:26 Comment(0)
B
2

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.
Bovid answered 28/10, 2009 at 21:33 Comment(0)
C
2

Just move your generate code to Form_Load event like this:

private void MainForm_Load(object sender, EventArgs e)
{
    Me.MyDataGridView.DataMember = "DataMember";
    Me.MyDataGridView.DataSource = "DataSource";
}
Cuxhaven answered 30/5, 2011 at 7:32 Comment(0)
P
1

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.

Pippas answered 26/2, 2009 at 1:43 Comment(1)
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.Bovid
R
0

I had the same problem

Private WithEvents _model As MyModel

removed the WithEvents and all was working again

Private _model As MyModel
Rapture answered 12/9, 2014 at 8:12 Comment(0)
B
0

For some reason error did not register in the error window until I manually opened up the "Designer" for my form. Once I opened the designer task I could then see the errors on bindings and such to manually remove.

What caused this is I removed datasources from my project, but it did not remove all the code behind them auto-magically I guess.

Barb answered 3/11, 2016 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.