C#MySql查询结果为combobox

时间:2011-03-01 18:50:59

标签: c# mysql winforms

我正在尝试查询MySql表,将所有结果放入组合框中。

所以我的查询结果是苹果2220
我想用apple2220填充组合框 我在使用datarow之外的字符串时遇到了麻烦。

        string MyConString = "SERVER=localhost;" +
                "DATABASE=iie;" +
                "UID=root;" +
                "PASSWORD=xxxx;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        string command = "select fruit,number from clientinformation";
        MySqlDataAdapter da = new MySqlDataAdapter(command,connection);
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach (DataRow row in dt.Rows)
        {
            string rowz = row.ItemArray.ToString();
        }
        connection.Close();

2 个答案:

答案 0 :(得分:3)

尝试以下几点:

...
foreach (DataRow row in dt.Rows)
{
   string rowz = string.Format("{0}:{1}", row.ItemArray[0], row.ItemArray[1]);
   yourCombobox.Items.Add(rowz);
}
....

答案 1 :(得分:2)

而不是

        foreach (DataRow row in dt.Rows)
        {
            string rowz = row.ItemArray.ToString();
        }

试试这个

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Fruit"; 
comboBox1.ValueMember = "Number";
comboBox1.DataBind();