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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0