Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using windows form. I have a list of table names. I want to display these name in combbox. It means when user click on combobox then all tables name should be listed for selection of one table.


What I have tried:

  List<string> listDiff = new List<string>();

                            if (!Utils.ComTable(livetables, testTables))
                            {
                                listDiff.Add(livetables.TableName);
                            }
Here i want to display listDiff in combobox

private void cboDatabase1_Click(object sender, EventArgs e)
        { 
//What code should be here to display listDiff
        }

Here cboDatabase1 method will be called when user will click on combobox.
Posted
Updated 26-Dec-17 0:56am

Items can be added to a Windows Forms combo box, list box, or checked list box in a variety of ways, because these controls can be bound to a variety of data sources. However,the simplest method no requires data binding.

To add items

1. by using the Add method of the ObjectCollection class. The collection is referenced using the Items property:
cboDatabase1.Items.Add("TableName1");
2. Insert the string or object at the desired point in the list with the Insert method:
cboDatabase1.Items.Insert(0,"TableName1");
3. Your case -> assign an entire array to the Items collection:
cboDatabase1.Items.AddRange(listDiff.ToArray());

C#
List<string> listDiff = new List<string>();
 
//... miss hier the loop to add items to your listDiff
if (!Utils.ComTable(livetables, testTables))
{
      listDiff.Add(livetables.TableName);
}
 
//now assign listDiff to the combobox
cboDatabase1.Items.AddRange(listDiff.ToArray());
 
Share this answer
 
v3
You must use combo.Items.Add(),also for front-end(design part)
you can use

<combobox>
<listitem>1<listitem>2<listitem>3
 
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