如何将自定义对象列表绑定到组合框?这就是我目前所拥有的:
this.classCmbo.DataSource = _viewmodel.Coarses; this.classCmbo.DisplayMember = "Name"; this.classCmbo.ValueMember = "Id";
我要显示什么“名称”,但我希望“Id”是与选择相关联的值.你怎么在winforms中做到这一点?
这是Coarse obj:
public class Coarse { public virtual int Id { get; private set; } public virtual string Name { get; set; } }
谢谢…
解决方法
你可以尝试这样…..
public class Country { public string Name { get; set; } public IList<City> Cities { get; set; } public Country(string _name) { Cities = new List<City>(); Name = _name; } } List<Country> countries = new List<Country> { new Country("UK"),new Country("Australia"),new Country("France") }; bindingSource1.DataSource = countries; comboBox1.DataSource = bindingSource1.DataSource; comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "Name";
我希望它会帮助你……