【问题标题】:filter a listbox bound to a acces database using a textbox使用文本框过滤绑定到访问数据库的列表框
【发布时间】:2015-10-22 12:21:56
【问题描述】:

我是 c# 的新手,正在上课,但是我正在努力做的事情,我知道我已经提前了很多。 我有一个带有列表框和文本框的表单。 这就是我填充列表框的方式

private void Centrale_Gegevens_Load(object sender, EventArgs e)
    try
    {
        OleDbConnection verbinding = new OleDbConnection();
        verbinding.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Het Vlaamse Kruis\Het Vlaamse Kruis\data\Patienten.accdb; Jet OLEDB:Database Password=internet;";
        verbinding.Open();
        OleDbCommand combo = new OleDbCommand();
        combo.Connection = verbinding;
        string query = "select NaamPatient from tbl_Patient";
        combo.CommandText = query;
        OleDbDataReader reader = combo.ExecuteReader();
        while (reader.Read())
        {
            lstBox.Items.Add(reader["NaamPatient"]);
        }
        verbinding.Close();

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error" + ex);
    }
}

列表框以这种方式填充人名。 名为 textbox1 的文本框是我想要用来过滤列表框的。

这是我得到的sofare,但它不起作用。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    OleDbConnection verbinding = new OleDbConnection();
    verbinding.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Het Vlaamse Kruis\Het Vlaamse Kruis\data\Patienten.accdb; Jet OLEDB:Database Password=internet;";
    verbinding.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from tbl_Patienten where NaamPatient like '" + textBox1.Text + "%' ";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    da.Fill(dt);
    lstBox.DataSource = dt;
    lstBox.DisplayMember = "NaamPatient";
    verbinding.Close();
}

我几乎可以在网上找到所有关于它的东西,不管我做什么,我都无法让它工作。 如果我在文本框中键入 A,列表框会显示所有以 A 开头的名称,如果我键入 AB,则列表框会显示以 AB 开头的所有内容等。 提前致谢

【问题讨论】:

标签: c# listbox


【解决方案1】:

首先,在 Centrale_Gegevens_Load 中,表的名称是 tbl_Patient,但在 textBox1_TextChanged 中,它是 tbl_Patienten。 其次,Connection 属性尚未初始化。 您必须在初始化 cmd 后插入:cmd.Connection = verbinding;

 OleDbCommand cmd = new OleDbCommand();
cmd.Connection = verbinding;

对不起,我的英语不好。

【讨论】:

  • OleDbCommand cmd = new OleDbCommand(); cmd.Connection = 绑定;那成功了,非常感谢。而且我的英语并不比你的好::-)
  • 投票它是一个答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
相关资源
最近更新 更多