Filtering items in a Listview
Asked Answered
B

4

6

I am trying to filter items in a ListView by using a TextBox.
I've managed to make something, but it can only delete items from my listview, not bring them back. Here is a little example of my code:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string value = textBox1.Text.ToLower();
    for (int i = listView1.Items.Count - 1; -1 < i; i--)
    {
        if
        (listView1.Items[i].Text.ToLower().StartsWith(value) == false)
        {
            listView1.Items[i].Remove();
        }
    }
}  

Does anybody has an idea on how to retrieve the deleted items? I can't seem to figure it out >:...

Brucie answered 14/5, 2013 at 18:1 Comment(2)
You can't. Items is an ordinary collection; once you remove the items, they're gone. You would have to store the removed items somewhere (perhaps in another collection) if you wanted to restore them later.Inconformity
How you bind the ListView? can you update the code related to data binding?Pectize
P
16

check below sample app

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
public partial class Form1 : Form
{
    // keep list of listview items 
    List<Data> Items = new List<Data>();

    public Form1()
    {
        InitializeComponent();
        // get initial data
        Items = new List<Data>(){
            new Data(){ Id =1, Name ="A"},
            new Data(){ Id =2, Name ="B"},
            new Data(){ Id =3, Name ="C"}
        };

        // adding initial data
        listView1.Items.AddRange(Items.Select(c => new ListViewItem(c.Name)).ToArray());
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        listView1.Items.Clear(); // clear list items before adding 
        // filter the items match with search key and add result to list view 
        listView1.Items.AddRange(Items.Where(i=>string.IsNullOrEmpty(textBox1.Text)||i.Name.StartsWith(textBox1.Text))
            .Select(c => new ListViewItem(c.Name)).ToArray());
    }

}

class Data
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Pectize answered 14/5, 2013 at 18:40 Comment(2)
NOTE: This does not work if you use a DataContext! ;)Gumma
This will not work with subitems and also lose any other featrues of the listviewitems like tags etc. Instead of the class a simple List<>ListViewItem> would be a much better storage.Valvulitis
C
2

You can change your logic and first search the items to delete, them delete they.

IList<Object> itemsToDelete = new List<Object>( listView1.find(delegate(string text){
     return !text.ToLower().StartsWith(value);
}));

listView1.Remove(itemsToDelete);
return itemsToDelete;

But you have to return another list. When you delete the items form the original list, you cant recovery It. You have to store it in another list.

Cristobalcristobalite answered 14/5, 2013 at 18:17 Comment(0)
P
1

After trying to find a solution and giving up on searching I created this simple method and it worked for me.

    public static void FilterList(ListView list, string text)
    {
        if (text.Length > 0)
        {
            foreach (ListViewItem item in list.Items)
            {
                if (!item.ToString().ToLower().Contains(text.ToLower()))
                {
                    list.Items.Remove(item);
                }
            }
        }
        else 
        {
            UpdateList(list);
        }
    }
Ptisan answered 12/11, 2022 at 4:57 Comment(0)
T
0
listBox1.Items.AddRange(listBox1.Items.Cast<String>    ().Where(X=>X.StartsWith(textBox1.Text)).ToArray());

Link

Works the same way with ListView.

Hope it helps

Tourneur answered 14/5, 2013 at 18:6 Comment(3)
I've changed the code into: ` listView1.Items.AddRange(listView1.Items.Cast<String>().Where(X => X.StartsWith(textBox1.Text)).ToArray());` but I get an error saying: (screenshot) -> puu.sh/2TpSG.pngBrucie
ListViewItem[] list = listView1.Where(X => X.StartsWith(textBox1.Text)).ToArray(); Now clear the listview and assign the new list to the listview: listView1.Items.Clear(); listView1 = list;Tourneur
cannot convert from 'string' to 'System.Windows.Forms.ListViewItem[]'Namnama

© 2022 - 2024 — McMap. All rights reserved.