【问题标题】:How to Reload the items after searching in listview如何在列表视图中搜索后重新加载项目
【发布时间】:2020-11-17 17:03:20
【问题描述】:

我想在列表视图中搜索 ic# 当我在搜索框中写东西时,我的文本出现在列表视图中并向我显示我需要的项目,但是当我从搜索文本框中删除文本时,我看不到我的旧数据在列表视图中我的代码是:

 private bool ItemMatches(ListViewItem item, string text)
        {
        bool matches = false;
        matches |= item.Text.ToLower().Contains(text.ToLower());
        if (matches)
        {
            return true;
        }
        foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
        {
            matches |= subitem.Text.ToLower().Contains(text.ToLower());
            if (matches)
            {
                return true;
            }
        }
        return false;
    }
  private void textBox4_TextChanged(object sender, EventArgs e)
        {
            listView1.BeginUpdate();

            // restore all items in case user deletes some characters in the textbox
         //   ReinitList();

            string search = textBox4.Text;
            // Search items in our Jobs ListView, remove those that do not match search
            if (search != String.Empty)
            {
                for (int i = listView1.Items.Count - 1; i >= 0; i--)
                {
                    ListViewItem currentItem = listView1.Items[i];
                    if (ItemMatches(currentItem, search))
                    {
                        // highlight, or move highlighting to ItemMatches
                    }
                    else
                    {
                        listView1.Items.RemoveAt(i);
                        }
                    }
                }
            listView1.EndUpdate();
        }

【问题讨论】:

  • 您正在删除项目。一旦删除,你就完成了。尝试将项目保留在列表中,然后将它们添加回来
  • 我会怎么做你能解释一下

标签: c# windows winforms listview search


【解决方案1】:

我已经制作了这个工作程序。它可以满足您使用列表集合的要求。您只需要添加您的子项搜索逻辑,它就会满足您的期望

public partial class Form1 : Form
{
    private List<ListViewItem> _items = new List<ListViewItem>();

    public Form1()
    {
        InitializeComponent();

        listView1.Columns.Add("Col 1");
        listView1.Columns.Add("Col 2");
        listView1.Columns.Add("Col 3");
        listView1.FullRowSelect = true;
        listView1.View = View.Details;

        _items.Add(new ListViewItem(new[] { "a item", "some text aaa-contained", "dfsdfsdf 3a sfdfsgerrge" }));
        _items.Add(new ListViewItem(new[] { "b item", "some text bbb-contained", "xcvcxbxcv 3b 56756855" }));
        _items.Add(new ListViewItem(new[] { "c item", "some text ccc-contained", "wefweferbtr 3c yjyuk" }));
        _items.Add(new ListViewItem(new[] { "d item", "some text ddd-contained", "ghjghkghgh 3d 5675675uytry" }));
        _items.Add(new ListViewItem(new[] { "e item", "some text eee-contained", "sdfasdfwewer 3e hgytuiti" }));

        listView1.Items.AddRange(_items.ToArray());
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        if (textBox1.Text == string.Empty)
        {
            listView1.Items.AddRange(_items.ToArray());
            return;
        }
        var searchResults = _items.Where(item => AnyOfTheItemsContainAGivenText(item, textBox1.Text));
        listView1.Items.AddRange(searchResults.ToArray());
    }


    private bool AnyOfTheItemsContainAGivenText(ListViewItem item, string text)
    {

        if (item.Text.IndexOf(text, 0, StringComparison.InvariantCultureIgnoreCase) > -1)
            return true;
        foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
        {
            if (subitem.Text.IndexOf(text, 0, StringComparison.InvariantCultureIgnoreCase) > -1)
                return true;
        }
        return false;
    }
}

【讨论】:

  • 您的代码很完美,但它只搜索索引 0 项数组中的修改条目
  • @irfankhan 我想,你会很高兴我为你留下了最后一个功能来声名鹊起。我只是向你展示了主要的想法。
  • 亲爱的主要问题是我没有得到这个(item.text)来自哪里我想搜索“名称”列所以如何搜索该列我的主要问题是这'
  • 您在“添加子项搜索”的代码中发表评论,所以我想添加子项 [5] 搜索只需告诉我那行代码我会这样做提前谢谢
  • @irfankhan 好的。我改进了我的程序以搜索所有列
猜你喜欢
  • 2017-02-17
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多