0

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

 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;

            }
        }
    }
4

3 回答 3

2

你能试试这个吗?

        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;
        }
于 2013-07-18T17:49:27.933 回答
0

如果您已将显示成员设置为:

ModelComboBox.DataSource = bindingSource3;
ModelComboBox.DisplayMember = "ColumnName";

而且它仍然显示有趣的值,它究竟显示了哪些值?

  • 请注意,在工具条中,您可能还必须这样做:

    ModelComboBox.BindingContext = this.BindingContext;

这是一个参考

于 2013-07-18T17:32:12.187 回答
0

尝试添加

ComboBox1.ItemsSource = bindingSource3

如果这是您的答案,则将其标记为答案

于 2013-07-18T17:40:12.690 回答