如何将组合框绑定到成员 [英] how to bind combobox to a member

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

问题描述

如何将特定的类成员绑定到组合框?

这是我的课程:

how do i bind a particular class member to the combobox?

here''a my class:

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



我在这里添加值



here''s ho i add values

List<Person> person = new List<Person>()
{
    new Person(){Name="Anna", Date=new DateTime(2006, 10, 01), Age=12},
    new Person(){Name="Ben", Date=new DateTime(2006, 11, 02), Age=13},
    new Person(){Name="Cora", Date=new DateTime(2006, 01, 03), Age=11},
    new Person(){Name="Dan", Date=new DateTime(2006, 02, 04), Age=11},
    new Person(){Name="Eve", Date=new DateTime(2006, 12, 20), Age=14},
    new Person(){Name="Fe", Date=new DateTime(2006, 12, 05), Age=15},
    new Person(){Name="George", Date=new DateTime(2006, 12, 06), Age=12},
    new Person(){Name="Helen", Date=new DateTime(2006, 12, 30), Age=13},
    new Person(){Name="Ivan", Date=new DateTime(2006, 12, 30), Age=15},
};





how can i make the the names in person as the content of my combobox?

推荐答案


为此:
1.在Windows窗体设计器中,转到组合框属性.
2.找到数据源属性并将其展开
3.单击添加新的项目数据源".
4.在向导中,选择对象",然后选择下一步".
5.在树中选择Person类(如果没有,请尝试使用Person类构建项目).
6.单击完成(新组件将添加到名为personBindingSource的表单中)
7.找到组合框属性DisplayMember.
8.从属性列表中选择以显示(名称)或键入它.

9.在Form_Load中放置:
Hi
To Do this:
1. In Windows Forms designer go to combobox properties.
2. Find datasource property and expand it
3. Click "Add new Project datasource".
4. In the wizard select Object and then Next.
5. Select Person class in the tree ( if you does not have it try to build project with Person class ).
6. Click finish ( New component will be added to the form named personBindingSource )
7. Find combobox property DisplayMember.
8. Select from the list you property to display ( Name ) or type it.

9. In the Form_Load put:
personBindingSource.DataSource = person; // initialized list of your persons



就是这样.



Thats it.


尝试这种方式

Try this way

List<Person> person = new List<Person>()
            {
                new Person(){Name="Anna", Birthday=new DateTime(2006, 10, 01), Age=12},
                new Person(){Name="Ben", Birthday=new DateTime(2006, 11, 02), Age=13},
                new Person(){Name="Cora", Birthday=new DateTime(2006, 01, 03), Age=11},
                new Person(){Name="Dan", Birthday=new DateTime(2006, 02, 04), Age=11},
                new Person(){Name="Eve", Birthday=new DateTime(2006, 12, 20), Age=14},
                new Person(){Name="Fe", Birthday=new DateTime(2006, 12, 05), Age=15},
                new Person(){Name="George", Birthday=new DateTime(2006, 12, 06), Age=12},
                new Person(){Name="Helen", Birthday=new DateTime(2006, 12, 30), Age=13},
                new Person(){Name="Ivan", Birthday=new DateTime(2006, 12, 30), Age=15},
            };
            comboBox1.DataSource = person;
            comboBox1.DisplayMember = "Name";



将此代码发布到您的Form Load事件中.

希望对您有帮助



Post this code in your Form Load event.

Hope this helps


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

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