如何将数据源绑定到ComboBox?

如何将数据源绑定到ComboBox?

问题描述:

所以我目前有两个组合框。一个正在制造,另一个正在制造。我的sql查询看起来像这样的从sheet1中选择不同的模型,其中(Manufacture = @ Manufacture)在我执行它并填充数据网格表时有效。但是,如果我尝试将其放入组合框,则会得到System.data.d ......等供我选择。我怎么能让它显示值而不是所有这一切。我在做什么错?

So i currently have two combo boxes. One Being Manufacture the other being model. My sql query looks like this "Select Distinct Model From sheet1 where (Manufacture =@Manufacture)" this works when i execute it and if i were to fill a datagridtable. But if i try to place this into a combobox i get System.data.d...... etc for my selections. How can i just have it show the values instead of all this. What am i doing wrong?

 private void ManuComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        string manu = comboBox3.Text;
        string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01;    Integrated Security=True";
        string sqlcmd = "SELECT DISTINCT Model FROM Sheet1 WHERE (Manufacture =@Manufacture)";
        using (SqlConnection conn = new SqlConnection(conStr))
        {

            SqlCommand cmd = new SqlCommand(sqlcmd, conn);

            cmd.Parameters.AddWithValue("@Manufacture", manu);

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Close();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);                    
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource3.DataSource = table;                   
                ModelComboBox.DataSource = bindingSource3;

            }
        }
    }


可以尝试一下吗?

        using (SqlConnection conn = new SqlConnection(conStr))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sqlcmd, conn);

            cmd.Parameters.AddWithValue("@Manufacture", manu);

            SqlDataReader dr = cmd.ExecuteReader();

            IList<string> modelList = new List<string>()
            while (dr.Read())
            {
                modelList.add(dr[0].ToString());
            }

            ModelComboBox.DataSource = modelList;
        }