本文介绍了如何为组合框(C#)设置mySQL数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,在设置组合框的数据源时遇到了一些严重的问题.我想要发生的事情如下:我希望在C#Windows论坛上的组合框中,使用mySQL数据库中表的一列中的字符串名称填充字符串.

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.

mySQL表具有以下格式:

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.

推荐答案

尝试一下:

comboBox1.DataSource = riverDataSet.Tables[0];
comboBox1.DisplayMember = "<column name>";
comboBox1.ValueMember = "<column name>";

这篇关于如何为组合框(C#)设置mySQL数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:50