[英]filter a listbox bound to a acces database using a textbox
I'm pretty new in c#, taking lessons but with what i'm trying to do i know that i'm way ahead of schedule. 我是C#的新手,上了课,但是我想做的事我知道我比计划提前了。 I have a form with a listbox and a textbox.
我有一个带有列表框和文本框的表单。 this is how I populate the listbox
这就是我填充列表框的方式
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);
}
}
the listbox is in that way populated with names of persons. 这样,列表框中便填充了人员名称。 The textbox named textbox1 is what i want to use to filter the listbox.
我想用来过滤列表框的是名为textbox1的文本框。
This is what i got sofare, but it doesn't work. 这就是我得到的沙发,但不起作用。
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();
}
I have red almost everything I can find on the net about it, bus no mather what i do, I can't get it to work. 我几乎可以在网上找到所有关于它的东西,我无法做任何事,我无法正常工作。 How can I get if I type A in the textbox that the listbox shows all the names beginning with A, And if I type AB that the listbox shows everything beginning with AB etc. Thanks in advance
如果我在文本框中键入A,列表框显示以A开头的所有名称,如果我键入AB,则列表框显示以AB开头的所有内容,如何获取。
Firstly, in Centrale_Gegevens_Load, table's name is tbl_Patient but in textBox1_TextChanged, it is tbl_Patienten. 首先,在Centrale_Gegevens_Load中,表的名称为tbl_Patient,而在textBox1_TextChanged中,表的名称为tbl_Patienten。 Secondly,Connection property has not been initialized.
其次,Connection属性尚未初始化。 you must insert this:
cmd.Connection = verbinding;
您必须插入:
cmd.Connection = verbinding;
after initializing the cmd; 初始化cmd之后;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = verbinding;
Sorry for my bad English. 对不起,我的英语不好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.