【发布时间】:2015-07-03 01:04:45
【问题描述】:
我在 Stack Overflow 上找到了这个,但我找不到答案。
我想将数据网格的 ComboBox 绑定到 property of a class,它返回 [one of the ] Enum Value。
MyEnum
{
[StringValue("SomeVal")]
SomeVal,
[StringValue("AnotherVal")]
AnotherVal,
[StringValue("OneMoreVal")]
OneMoreVal
}
class MyClass
{
public MyEnum A_Value
{
return whatever; // whatever is MyEnum type
}
}
现在我创建了一个带有组合框的数据网格列,我需要在其中绑定一个属性
myCombo.DataSource = Enum.GetValues(typeof(MyEnum));
myCombo.DataBindings.Add("SelectedValue", myDataSource, bindingPath + ".A_Value");
当我运行这段代码时,它失败并出现错误
“无法为没有 ValueMember 的组合框设置值”
然后我在下面添加一行
myCombo.ValueMember = "Value";
这次没有失败,但是没有设置选择值。 有人可以帮我解决这个问题吗?
我提到的:
【问题讨论】:
-
您已标记为 WPF,但这看起来像 WinForms,并且一些链接是针对 WinForms 的。是哪个?
-
@CharlesMager 道歉,它是 WinForms
-
这对我有帮助 --> stackoverflow.com/questions/983350/…
标签: c# winforms c#-4.0 datagrid