【问题标题】:ComboBox doesn't show the dataComboBox 不显示数据
【发布时间】:2015-09-17 20:28:35
【问题描述】:

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;
  • 你愿意Sql注入使用参数化查询

标签: c# visual-studio combobox userform


【解决方案1】:

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

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

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

如果组合框仍为空,则问题的根源在于获取数据并填充数据源。

【讨论】:

    【解决方案2】:

    在这一行下断点:

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

    然后在监视或快速监视窗口中查看 myDataSet.Tables["comboBox6"] 的值。有行吗?

    同时更改您的代码:

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

    在那里下断点,看看sql的值是什么。

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

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 2013-03-21
      • 1970-01-01
      • 2017-08-11
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      相关资源
      最近更新 更多