C# 绑定到列表的组合框的MemberValue是多少<;字符串>;?
绑定到C# 绑定到列表的组合框的MemberValue是多少<;字符串>;?,c#,list,winforms,data-binding,combobox,C#,List,Winforms,Data Binding,Combobox,绑定到列表的组合框的值是多少 我正在使用windows窗体和.NET Framework 4 cmbForms.DataSource = Forms; cmbForms.ValueMember="System.String"; if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form)) { cmbForms.SelectedValue = PhotoDescription.Details.Form;
列表的组合框的值是多少
我正在使用windows窗体和.NET Framework 4
cmbForms.DataSource = Forms;
cmbForms.ValueMember="System.String";
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedValue = PhotoDescription.Details.Form;
}
其中,表格
为:
public List<string> Forms { get; set; }
公共列表表单{get;set;}
来自
如果ValueMember中未指定属性,则SelectedValue返回
对象的ToString方法的结果
根据更新进行编辑
您的代码将得到一个ArgumentException
,因为System.String
不是可以解析的属性(您的String
对象没有名为System.String
的属性)。默认值from将是空字符串(“”)
在这种情况下,您不需要设置ValueMember
属性,因此可以使用SelectedItem
cmbForms.DataSource = Forms;
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedItem = PhotoDescription.Details.Form;
}
我想应该是System.String
。为什么不自己试一试?”“出现错误无法在ValueMember为空的ListControl中设置SelectedValue。是的,您没有设置ValueMember
,因此您会出现错误。您想做什么,可能有不同的方法?整个代码都有问题,只需尝试根据简单的if条件设置组合框的值。然后使用cmbForms。SelectedItem
而不是cmbForms。SelectedValue
,因为您不需要设置ValueMember
属性。(见最新答案)