8

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.

dav_i
  • 27,509
  • 17
  • 104
  • 136
Evgeny
  • 3,320
  • 7
  • 39
  • 50
  • 1
    Possibly related to [Databinding to the properties of an object that implements IEnumerable](http://stackoverflow.com/q/1336395/167897) – Wernight Jul 31 '12 at 10:49

3 Answers3

13

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

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.

dav_i
  • 27,509
  • 17
  • 104
  • 136
Evgeny
  • 3,320
  • 7
  • 39
  • 50
0

One of the possible reason for this error is table/ Dataset do not have specified column. Specially, in case of Typed DataSet make sure you have proper names in XSD matching with column names from table