Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how do i bind a particular class member to the combobox?

here'a my class:
C#
public class Person
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public int Age  { get; set; }
}


here's ho i add values
C#
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?
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 16:35pm    
Tag it! WPF, Forms, APS.NET, what?
--SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 16:36pm    
How do you know it Forms?
--SA
Marrakech 2-Jun-11 4:14am    
Probably suggestion
Marrakech 2-Jun-11 4:16am    
Also if you need to combobox will be refreshed each time you doing Add or Remove from your list you can use BindingList<Person> instead List<Person>.
Try this way

C#
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";


Post this code in your Form Load event.

Hope this helps
 
Share this answer
 
v2
Comments
Member 7838027 1-Jun-11 10:34am    
thanks :)
Member 7838027 1-Jun-11 20:39pm    
hi! when, i added a new person, the combobox is not updated. how can i update the combobox? i am sure that the new person that i added is successfully added to the list because it is displayedin the gridview.
Wayne Gaylard 2-Jun-11 2:43am    
Once you have updated the List then you need to rebind the comboBox to the list. My suggestion would be to have a separate method that binds the comboBox to the List, and then every other method that updates the List can then just call the bind method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900