Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a combo box (cboTable) that contains list of database tables. When I select a table, it should generate a number of combo boxes that contains table columns name. If the table has 4 columns then 4 combo boxes should be created with each of columns name as the default value of each combo box.

I want to change the current codes so I can create combo boxes and populate its items dynamically. Could someone helps me to do this? Thank you.

private void cboTable_SelectedIndexChanged(object sender, EventArgs e)
        TableID = cboTable.SelectedItem.ToString();
        SqlCommand cmd;
        SqlDataReader dr;

        SqlConnection CS = new SqlConnection();
        CS.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conel"].ConnectionString;
        cmd = new SqlCommand("select Column_name from Information_schema.columns where Table_name = @TableID");
        cmd.Connection = CS;
        cmd.Parameters.AddWithValue("@TableID", cboTable.SelectedItem.ToString());
        dr = cmd.ExecuteReader();


        while (dr.Read())

        cboON1.SelectedIndex = 0;
        cboON2.SelectedIndex = 1;
        cboON3.SelectedIndex = 2;
        cboON4.SelectedIndex = 3;



What I have tried:

I created the following codes but it only populate column names in the first combo box while the other comboboxes are empty.

for (int i = 1; i < 5; i++)
                    ComboBox cb = new ComboBox();
                    cb.Location = new Point(475, 25 * i + 210);
                    switch (i)
                        case 0:
                            cb.Click += new EventHandler(ButtonClick2);


                    while (dr.Read()) 
Updated 1-Oct-20 22:47pm
BillWoodruff 2-Oct-20 0:16am
If there are four DataTables, and you select one of them ... and, you want a second ComboBox to show the Column names of the selected Table: why do you need to show four additional ComboBoxes rather than one ?
Daniel Vero 2-Oct-20 0:20am
Because I need to compare every combo box with the other combo boxes that show the column name from the excel file.
BillWoodruff 2-Oct-20 22:44pm
I am still unclear as to what your intent is here. You select a DataTable in one ComboBox; then, you create new ComboBoxes for each Column in the selected DataTable. You add the column name to the new ComboBoxes.

1. does every DataTable have the same number of Columns ? Always #4 ?

2. then what: what else goes into the newly created ComboBoxes ?

1 solution

Follow the steps from MSDN: Add controls to Windows forms by Visual C# - C# | Microsoft Docs[^]


First of all, add this line in the top of Form:
//this is needed for later use
List<ComboBox> comboBoxes  = new List<ComboBox>();

Second, add this procedure:
private void RemoveExistingComboBoxes()
    foreach (ComboBox cb in comboBoxes)

Finally, in a cboTable_SelectedIndexChanged event:

DataTable dt = new DataTable();
string tid = cboTable.SelectedItem.ToString();
string connString  = CS.ConnectionString = ConfigurationManager.ConnectionStrings["conel"].ConnectionString;

using(SqlConnection CS = new SqlConnection(connString))
	using(SqlCommand cmd = new SqlCommand("select Column_name from Information_schema.columns where Table_name = @TableID", CS))
	    cmd.Parameters.AddWithValue("@TableID", tid);
		using(SqlDataReader reader = cmd.ExecuteReader())
for(int i = 0; i<dt.Rows.Count; i++)
	ComboBox cb = new ComboBox();
	//populate other properties here
	cb.Parent = this;

That's all. Good luck!
BillWoodruff 2-Oct-20 22:37pm
My vote of #1: the OP's code shows he knows how to add Controls to a Form.
Maciej Los 4-Oct-20 3:28am
Bill, take a look at OP's code. His cboTable_SelectedIndexChanged procedure does not refer to the code where comboboxes are creating. He call Clear() method for constant set of comboboxes {cboON1, cboON2, cboON3, cboON3}.
As to me, OP is using few pieces of code having NO idea how to bind them each other.
BillWoodruff 4-Oct-20 4:48am

He knows how to add Controls to a Form.

"As to me, OP is using few pieces of code having NO idea how to bind them each other."

If that's what you think the real issue is: then, I would expect you would post a solution that addresses that.
Maciej Los 5-Oct-20 1:55am
You're trying to involve me to improve my answer, but i'm lazy... ;)
BillWoodruff 5-Oct-20 2:07am
Laziness guarantees a down-vote :)
Maciej Los 5-Oct-20 10:08am
Bill, you gave me the reasons motivation to improve my answer. Does it acceptable, now?
Sandeep Mewara 16-Oct-20 15:25pm
Maciej Los 16-Oct-20 15:27pm
Thank you, Sandeep.

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