ComboBox 不显示数据

ComboBox doesn't show the data

ComboBox 不显示数据,我用这种方式用数据库中的数据填充组合框:

private void PartDefective(string id)
{
    cmd = new SQLiteCommand("Select * FROM Part_defective where testers = '" + id + "'", DBcon);
    if (DBcon.State == ConnectionState.Closed)
        DBcon.Open();
    myDA = new SQLiteDataAdapter(cmd);
    myDataSet = new DataSet();
    myDA.Fill(myDataSet, "comboBox6");
    this.comboBox6.DataSource = myDataSet.Tables["comboBox6"].DefaultView;
    this.comboBox6.ValueMember = "Part";
    this.comboBox6.DisplayMember = "Part";
    this.comboBox6.SelectedItem = "ID";
    this.comboBox6.SelectedIndex = -1;

    DBcon.Close();
}

并显示我使用的数据库中的数据:

private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.comboBox6.SelectedValue == null)
    {
        testerid = "1";
    }
    else
    {
      part = this.comboBox6.SelectedText.ToString();
    }
}

在这一行打断点:

this.comboBox6.DataSource = myDataSet.Tables["comboBox6"].DefaultView;

然后在watch或quickwatchwindow中查看myDataSet.Tables["comboBox6"]的值。有行吗?

同时更改您的代码:

string sql = "Select * FROM Part_defective where testers = '" + id + "'";
cmd = new SQLiteCommand(sql, DBcon);

打个断点,看看sql的值是多少

手动 运行 sql 对照您的数据库,看看是否有任何结果。

绝对没有理由定义SelectedItemSelectedIndexSelectedIndexChanged Event 对于显示组合框也是完全多余的。

删除这些行,看看它是否解决了您的问题。

this.comboBox6.SelectedItem = "ID";
this.comboBox6.SelectedIndex = -1;

如果组合框仍然是空的,问题的根源必须是获取数据和填充数据源。