Click here to Skip to main content
15,666,029 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can add a series of items to a comboBox as follows:-
foreach (DataRow row in Variables.ClientsTable.Rows)
    string temp1 = row["CompanyName"].ToString();
    string temp2 = row["CompanyID"].ToString();

However the comboBox then drops down a blank list, which looks to be of the right length. I assume I need to set the DisplayMember and other properties, but how do I select the just added item to do this?

What I have tried:

I started by assigning the DataTable to the comboBox as a datasource. However I have a number of comboboxes all using this same data and I found that changing one changes them all.
I then tried copying the table into a set of Lists to be used, one for each comboBox. Same problem as I have now, so I tried binding Source.
Same problem.
I'm now at the stage of simply adding the items "manually" from the table to keep things simple.
Updated 13-Jan-22 10:16am

you must reset the binding after you add new items at run-time. working code example:
ar inrange0 = new List<int>
    1, 2, 3, 4, 5, 6, 7, 8

comboBox1.DataSource = inrange0;
comboBox1.DataSource = null;
comboBox1.DataSource = inrange0;
If you are not using binding: then, all you need to do is add new items:
// since 'inrange0' is a list of Int, cast and 'ToArray required


// add another item
Share this answer
ormonds 13-Jan-22 2:50am    
But if I am not using a DataSource for the comboBox, simply adding Items one by one in a foreach loop, then the DataSource is always null. I've tried Refreshing the comboBox, but the datalist is still blanks.
I assume that having added the comboBox item as per my code I have to add a value for that item's DisplayMember, but can't see how to select that item:
comboBoxProjects20. ?? . DisplayMember = temp1.
BillWoodruff 13-Jan-22 3:49am    
see second example added to solution.
ormonds 13-Jan-22 15:59pm    
Thank you. That is what I am doing, but then when the comboBox is used it shows a list of blank items. Do I need to add a DisplayMember property to the comboBox and if so how do I do that? If not, any idea why the items aren't showing?
BillWoodruff 14-Jan-22 4:00am    
You use DisplayMember and ValueMember ONLY when you use binding. If you ARE using binding: reset them after you add items and reset the DataSource and see if that makes a difference.
Found it,
Thanks for your help.
Share this answer

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