Reputation: 1550
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.
Upvotes: 0
Views: 3029
Reputation: 2458
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;
Upvotes: 0
Reputation: 33381
Try this:
comboBox1.DataSource = riverDataSet.Tables[0];
comboBox1.DisplayMember = "<column name>";
comboBox1.ValueMember = "<column name>";
Upvotes: 3