Click here to Skip to main content
16,012,611 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I would like to ask if you know how to populate a programatically made combobox. What I mean in programatically is that it is not in the designer. It will mean a lot.

Thank you! :)
Posted
Updated 21-Oct-13 0:21am
v2
Comments
Kenneth Haugland 21-Oct-13 12:49pm    
WinForm or WPF?

Or you can set the Datasource of the combobox and populate it directly from a datatable, for example:

VB
cbCountry.DataSource = CustomSQL.GetCountryList
cbCountry.DisplayMember = "Country"
cbCountry.ValueMember = "Country"
cbCountry.Text = cbCountry.Items(226).ToString


This example populates a combobox with a list of countries from a datatable returned from 'CustomSQL.GetCountryList' and then preselects UK (item 226 in the list) as the text.
 
Share this answer
 
You populate it calling the Add method of its Items property, e.g.
VB
myComboBox.Items.Add("first item")
 
Share this answer
 
What have you tried to get your answer? I assume nothing... since this is a really basic task.
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2[^]
 
Share this answer
 
Comments
Janna Dela Cruz 21-Oct-13 4:50am    
i actually have a working codes that populate combo box from database but when I apply it to my recent project where the combo box is not in the designer it doesn't work. it don't reflect in the combo box although the datasource is not empty.
Zoltán Zörgő 21-Oct-13 4:57am    
In general there is no automatic binding between them. But it depends on the technology you use. So, you have either to:
- bind the datasource to the combobox, or
- parse the datasource and add every item one by one

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