简体   繁体   English

使用文本框过滤绑定到acces数据库的列表框

[英]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.

相关问题 使用TextBox实时过滤ListBox - Filter ListBox with TextBox in realtime 使用文本框中的值过滤绑定到SQL数据库的Datagrid - Filter Datagrid bound to SQL Database with Value From Textbox 如何使用C#上的文本框删除/过滤列表框上的数据 - How to remove / filter data on listbox using textbox on C# 基于文本框的文本过滤列表框的项目,仅使用 WPF 中的 XAML - Filter items of a ListBox based on the text of a TextBox using only XAML in WPF 使用Access数据库从列表框到文本框显示具有多个值的项目 - display item with multiple values from listbox into textbox using access database 如何使用C#中的listBox将本地数据库项添加到textBox中 - How to add local database items into textBox using listBox in C# 集合ViewSource通过在文本框中键入内容来过滤列表框 - Collection ViewSource to filter listbox by typing in textbox 如何使用XAML中的TextBox和ViewModel,MVVM Light中的代码过滤ListBox - How to filter ListBox using TextBox from XAML and code in ViewModel, MVVM Light 使用带有2个选择的下拉列表对绑定到ObjectDataSource的ListBox进行排序 - Sorting a ListBox bound to an ObjectDataSource using a dropdown with 2 selections 如何在C#中使用本地数据库中的数据在textBox中显示列表框项目 - How to show listbox item in textBox using data from local database in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM