Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到列表的组合框的MemberValue是多少<;字符串>;?_C#_List_Winforms_Data Binding_Combobox - Fatal编程技术网

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
属性。(见最新答案)