0

I am a beginner at C# and am having some serious issues setting the data source of a combo box. What I want to have happen is as follows: I want the combo box on my C# windows forum to be populated with the string names in just one column of a table in my mySQL database.

The mySQL table has the following format:

river_id, river_name, ....... (other columns) 
_____________________________________________
1           river1
2           river2         
3           river3
4           river4
5           river5
6           river6

What I want to happen is have the combo box be populated with each river name. Here is my attempt:

 string query = "SELECT * FROM sources";
       MySqlDataAdapter riverSourcesAdapter = new MySqlDataAdapter(query,connectionString);
       DataSet riverDataSet = new DataSet();
       riverSourcesAdapter.Fill(riverDataSet);

       comboBox1.Text = riverDataSet.Tables[0].Rows[0][0].ToString();

I also tried setting the combo box datasource and datamember in the designer instead, but that approach did not seem to work either.

2 Answers 2

3

Try this:

comboBox1.DataSource = riverDataSet.Tables[0];
comboBox1.DisplayMember = "<column name>";
comboBox1.ValueMember = "<column name>";
Sign up to request clarification or add additional context in comments.

2 Comments

Well that was simple..... It works perfectly, thank you! Could you explain the need for the '<>' symbols please?
this is just placeholder for real column name :)
0

your code should be like this..

comboBox1.DataSource = riverDataSet;
comboBox1.DisplayMember = "river_name";
comboBox1.ValueMember = "river_id";
comboBox1.SelectedIndex = -1;
comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.