【问题标题】:Combobox items based on input from two other comboboxes基于来自其他两个组合框的输入的组合框项
【发布时间】:2017-05-02 07:16:13
【问题描述】:

您好,我有一个名为设备表的访问表和一个带有多个组合框的获胜表单,前三个工作但它们从访问表中获得了不同的值。然而,第三个是接收来自组合框 Manufacturer_cmbBx 和 Type_cmbBx 的输入,它们可以工作。我对所有组合框都使用了相同的代码,只有 Select 查询发生了变化。 VS 中的诊断工具显示正确的值被传递到 Select 查询中。但是组合框仍然是空的。我已经要求在组合框 Type_cmbBx 的 SelectedValueChanged 事件上更改组合框

     private void LoadModel_cmbBx()
    {

        string strCon = Properties.Settings.Default.Database2ConnectionString;
        using (OleDbConnection conn = new OleDbConnection(strCon))
        {
            try
            {
                string strSql = "Select Model from EquipmentTable where [Manufacturer] = '" + Manufacturer_cmbBx.Text + "' and [Type] = '" + Type_cmbBx.Text + "'";
                OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                Model_cmbBx.DataSource = ds.Tables[0];
                Model_cmbBx.ValueMember = "Model";
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
    }

【问题讨论】:

  • ds.Tables[0] 中包含的数据是什么样的?它是否包含多个列?
  • 设备表包含 ID、制造商、型号、类型和零件编号列。由于 DataSet ds 基于 EquipmentTable 它应该包含相同的内容。但我不知道如何检查。
  • 我会先尝试将数据放在List<> 中,以解决发生的问题。

标签: c#


【解决方案1】:

在 VS 中使用数据集查看器我可以看到只有模型列在那里,所以我将我的语句更改为

  "Select DISTINCT * from EquipmentTable where Type  = '" + Type_cmbBx.Text + "' and Manufacturer = '"+ Manufacturer_cmbBx + "'";

这仍然不起作用,但是

    "Select DISTINCT * from EquipmentTable where Type  = '" + Type_cmbBx.Text + "'";

照样工作

     string strSql = "Select DISTINCT * from EquipmentTable where Manufacturer = '" + Manufacturer_cmbBx + "'";

只有当我尝试添加第二个组合框时,数据集才会保持为空。关于为什么会这样的任何想法?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多