开发者

C# MySql query result to combobox

I'm trying to query an MySql table, put all results into a combobox.

So my query results in apple 2220

I want to populate the combobox with apple2220

i'm having trouble getting the string outside of 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 DataTab开发者_StackOverflow中文版le();
        da.Fill(dt);
        foreach (DataRow row in dt.Rows)
        {
            string rowz = row.ItemArray.ToString();
        }
        connection.Close();


Try something along these lines:

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


Instead of

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

try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜