组合框数据绑定引起的ArgumentException [英] ComboBox DataBinding causes ArgumentException

查看:121
本文介绍了组合框数据绑定引起的ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我几类对象:


I several objects of class:

class Person
{
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return Name + "; " + Sex + "; " + Age;
    }
}

和具有类型的属性<一类code>人:

class Cl
{
    public Person Person { get; set; }
}

和我要绑定 Cl.Person 来组合框。当我尝试做这样的:

And I want to bind Cl.Person to combobox. When I try to do it like this:

Cl cl = new cl();
comboBox.DataSource = new List<Person> {new Person{Name = "1"}, new Person{Name = "2"}};
comboBox.DataBindings.Add("Item", cl, "Person");



我得到一个的ArgumentException 。我应该如何修改我的结合来获得正确的程序的行为吗?结果
提前感谢!

I get an ArgumentException. How should I modify my binding to get the correct program behavior?
Thanks in advance!

推荐答案

绑定为的SelectedItem:

Bind to "SelectedItem":

        var persons = new List<Person> { new Person() { Name = "John Doe"}, new Person() { Name = "Scott Tiger" }};
        comboBox1.DisplayMember = "Name";
        comboBox1.DataSource = persons;
        comboBox1.DataBindings.Add("SelectedItem", cl, "Person");

这篇关于组合框数据绑定引起的ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆