Meg*_*ind 2 c# data-binding combobox list winforms
与ValueMambera ComboBox绑定的a是List<string>什么?
我正在使用Windows窗体和.NET Framework 4。
cmbForms.DataSource = Forms;
cmbForms.ValueMember="System.String";
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedValue = PhotoDescription.Details.Form;
}
Run Code Online (Sandbox Code Playgroud)
哪里Forms是:
public List<string> Forms { get; set; }
Run Code Online (Sandbox Code Playgroud)
从MSDN
如果在ValueMember中未指定属性,则SelectedValue返回对象的ToString方法的结果。
根据更新进行编辑
您ArgumentException的代码会附带一个,因为System.String它不是可以解析的属性(您的string对象没有名为的属性System.String)。MSDN的默认值将是一个空字符串("")。
在这种情况下,您无需设置ValueMember属性,因此可以SelectedItem代替使用。
cmbForms.DataSource = Forms;
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedItem = PhotoDescription.Details.Form;
}
Run Code Online (Sandbox Code Playgroud)