DataMember property [ObjectName] cannot be found on the DataSource

16,734

Solution 1

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.

Solution 2

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.

Solution 3

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";
}

Solution 4

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.

Share:
16,734
ptutt
Author by

ptutt

.Net Software Developer

Updated on August 10, 2022

Comments

  • ptutt
    ptutt over 1 year

    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?

  • ptutt
    ptutt about 15 years
    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.