Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bind to the property or column Name on the DataSource. Parameter name: dataMember

This is the exception that I'm getting when I'm trying to bind to a System.Type.Name.

Here is what I'm doing:

this.propertyTypeBindingSource.DataSource = typeof(System.Type);

/* snip */

this.nameTextBox1.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyTypeBindingSource, 
        "Name", true));

Is there some trick with binding to System.Type, is it not allowed or is there any workaround? Have no problems with binding to other types.

like image 332
Evgeny Avatar asked Oct 21 '08 00:10

Evgeny


2 Answers

Indeed, there is special treatment of Type... this approach is used in the IDE etc to configure meta-data ahead of time. If you look at IDE-generated bindings, they do things like:

bindingSource1.DataSource = typeof(MyObject);

saying "when we get real data, we expect MyObject isntance(s)"; i.e. when you ask for "Name", it is looking for the name property on MyObject - not the Name of the Type instance. This allows grids etc to obtain their metadata without having to wait for the real data; but as a consequence you can't bind to Type "for real".

The System.ComponentModel code is identical between simple bindings and list bindings (give or take a currency manager), so simple bindings also inherit this behaviour. Equally, you can't bind to properties of a class that implements IList/IListSource, since this is interpreted in a special way.

Your extra class seems a reasonable approach.

like image 81
Marc Gravell Avatar answered Oct 02 '22 12:10

Marc Gravell


Found a workaround. Made a class

public class StubPropertyType
{
    public StubPropertyType(Type type)
    {
        this.StubPropertyTypeName = type.Name;
    }

    public string StubPropertyTypeName = string.Empty;
}

created a binding source

this.propertyStubBindingSource.DataSource = typeof(StubPropertyType);

created an instance of the class and bound the textbox to it.

this.nameTextBox.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyStubBindingSource, 
        "StubPropertyTypeName", 
        true));

works exactly as required.

like image 39
Evgeny Avatar answered Oct 02 '22 12:10

Evgeny