C# 无法绑定到 DataSource 上的属性或列 Name。参数名称:dataMember
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/220392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot bind to the property or column Name on the DataSource. Parameter name: dataMember
提问by Evgeny
This is the exception that I'm getting when I'm trying to bind to a System.Type.Name.
这是我尝试绑定到 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.
绑定到 System.Type 是否有一些技巧,是不允许的还是有任何解决方法?绑定到其他类型没有问题。
采纳答案by Evgeny
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.
完全按照要求工作。
回答by Marc Gravell
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:
确实,Type 有特殊的处理方式……这种方法在IDE 等中用于提前配置元数据。如果您查看 IDE 生成的绑定,它们会执行以下操作:
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".
说“当我们获得真实数据时,我们期望 MyObject istance(s)”;即,当您要求“名称”时,它正在寻找MyObject 上的名称属性- 而不是 Type 实例的名称。这允许网格等无需等待真实数据即可获取其元数据;但因此,您无法绑定到“真正的”类型。
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.
System.ComponentModel 代码在简单绑定和列表绑定(给予或接受货币管理器)之间是相同的,因此简单绑定也继承了这种行为。同样,您不能绑定到实现 IList/IListSource 的类的属性,因为这是以特殊方式解释的。
Your extra class seems a reasonable approach.
您的额外课程似乎是一种合理的方法。
回答by Siddhanath Lawand
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
此错误的可能原因之一是表/数据集没有指定的列。特别是,在 Typed DataSet 的情况下,请确保 XSD 中有正确的名称与表中的列名匹配