1 Year
Discussion Span
Last Post by ramkishor


The following is an example of how you can create a dynamic ComboBox and bind some data to it, you will need to modify according to your database, this example uses an SQL Server database:

ComboBox myDynamicComboBox;

private void Form1_Load(object sender, EventArgs e)
    //Create the combo box and add it to the form
    myDynamicComboBox = new ComboBox();
    myDynamicComboBox.Location = new System.Drawing.Point(100, 100);


    //Add an event handler to the combo box so that we can display some information when the selected index changes
    myDynamicComboBox.SelectedIndexChanged += myDynamicComboBox_SelectedIndexChanged;

    //Get data and bind to the combo box, change your connection string and query accordingly
    string connectionString = @"Server=DJJEAVONS-PC\SQLEXPRESS;Database=Test;Trusted_Connection=True;";
    string selectStatement = "SELECT ID, SomeTextColumn FROM Test_Table";

    using(SqlDataAdapter adapter = new SqlDataAdapter(selectStatement, connectionString))
        DataTable table = new DataTable();

        //Bind the DataTable to the ComboBox
        myDynamicComboBox.DataSource = table;
        myDynamicComboBox.DisplayMember = "SomeTextColumn";
        myDynamicComboBox.ValueMember = "ID";


void myDynamicComboBox_SelectedIndexChanged(object sender, EventArgs e)
    //Display the ID and SomeTextColumn values from the combo box
    MessageBox.Show(String.Format("{0}-{1}", myDynamicComboBox.SelectedValue.ToString(), myDynamicComboBox.Text));

In terms of inserting data back into the database, you have the values available to you regarding the selected item, as demonstrated in the SelectedIndexChanged event so it should be fairly straightforward to write data back to the database.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.