Follow the steps from MSDN:
Add controls to Windows forms by Visual C# - C# | Microsoft Docs[
^]
[EDIT]
First of all, add this line in the top of Form:
List<ComboBox> comboBoxes = new List<ComboBox>();
Second, add this procedure:
private void RemoveExistingComboBoxes()
{
foreach (ComboBox cb in comboBoxes)
this.Controls.Remove(cb);
comboBoxes.Clear();
}
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))
{
CS.Open();
cmd.Parameters.AddWithValue("@TableID", tid);
using(SqlDataReader reader = cmd.ExecuteReader())
dt.Load(reader);
}
RemoveExistingComboBoxes();
for(int i = 0; i<dt.Rows.Count; i++)
{
ComboBox cb = new ComboBox();
cb.Parent = this;
comboBoxes.Add(cb);
}
That's all. Good luck!