如何根据变量搜索列表框项目?

How can I search a listbox items depending on a variable?

我是 c# 的新手,正在尝试按如下方式搜索列表框: 首先我有这个:

 public partial class FrmCodes : Form
 {
        ...
        SqlConnection Cn = new SqlConnection(@"Server = AMR-PC\SQLEXPRESS ; Database=PlanningDB ; Integrated Security = True");
        SqlCommand cmd;
        SqlDataReader DataRead;
        ...

    public FrmCodes()
    {
        InitializeComponent();
        cmd = new SqlCommand("Select Item from Items", Cn);
        Cn.Open();
        DataRead = cmd.ExecuteReader();

        while (DataRead.Read())
        {
            ListItems.Items.Add(DataRead["Item"].ToString());
        }
        DataRead.Close();
        Cn.Close();
    }

并尝试这样做:

private void txtSrch_TextChanged(object sender, EventArgs e)
{
    ListItems.Items.Clear();
    while (DataRead.Read())
    {
        string str = DataRead["Item"].ToString();
        string srch = txtSrch.Text;
        if (str.Contains(srch))
        {
            ListItems.Items.Add(str);
        }
    }
}

它没有用,我尝试创建一个新的 sql select 查询来根据 txtSrch.Text 获取数据,但也一无所获。 提前致谢。

编辑#1 这是我之前提到的查询:

private void txtSrch_TextChanged(object sender, EventArgs e)
{
    ListItems.Items.Clear();

    SqlConnection Cn2 = new SqlConnection(@"Server = AMR-PC\SQLEXPRESS ; Database=PlanningDB ; Integrated Security = True");
    Cn2.Open();

    string srch = txtSrch.Text;
    using (SqlDataAdapter a2 = new SqlDataAdapter("Select Item from Items WHERE Item LIKE '%" + srch + "%'", Cn2))
    {
        var t2 = new DataTable();
        a2.Fill(t2);

        ListItems.DisplayMember = "Item";
        ListItems.ValueMember = "Code";
        ListItems.DataSource = t2;
    }
}

这不影响列表框中的项目 txtSrch 更改没有任何变化。

基于@Olivier Jacot-Descombes' 的评论,这对我有用:

private void txtSrch_TextChanged(object sender, EventArgs e)
{
    ListItems.DataSource = null;

    SqlConnection Cn2 = new SqlConnection(@"Server = AMR-PC\SQLEXPRESS ; Database=PlanningDB ; Integrated Security = True");
    Cn2.Open();

    string srch = txtSrch.Text;
    using (SqlDataAdapter a2 = new SqlDataAdapter("Select Item from Items WHERE Item LIKE '%" + srch + "%'", Cn2))
    {
        var t2 = new DataTable();
        a2.Fill(t2);

        ListItems.DisplayMember = "Item";
        ListItems.ValueMember = "Code";
        ListItems.DataSource = t2;
    }
}

另一个使用 Dataview 的解决方案感谢@Trevor

public partial class FrmCodes : Form
{
        ...
        SqlConnection Cn = new SqlConnection(@"Server = AMR-PC\SQLEXPRESS ; Database=PlanningDB ; Integrated Security = True");
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        ...

    public FrmCodes()
    {
        InitializeComponent();

        da = new SqlDataAdapter("Select Item from Items", Cn);
        da.Fill(dt);
        DataView dv = new DataView(dt);
        ListItems.DataSource = dv;
        ListItems.DisplayMember = "Item";
    }

文本框更改:

private void txtSrch_TextChanged(object sender, EventArgs e)
{

    ListItems.DataSource = null;
    DataView dv = new DataView(dt);
    string srch = txtSrch.Text;
    dv.RowFilter = string.Format("Item Like '%{0}%'", srch);
    ListItems.DataSource = dv;
    ListItems.DisplayMember = "Item";
}

谢谢。