I want to write a method to filter a listview
IEnumerable<ListViewItem> CurrentList;
CurrentList = ListViewExemple.Items.Cast<ListViewItem>();
var result = CurrentList.Select(i => i.Text.Contains(SearchTxtBox.Text));
Now How can i add the "result" items to a cleared Listview
You'll have to keep in mind that a ListViewItem has an owner, you need to make a copy of the item to put it in another listview. Easy to do with its Clone() method. Or move it from one to the other, not likely in this case. So you probably want this:
var matches = listView1.Items.Cast<ListViewItem>()
.Select(item => (ListViewItem)item.Clone())
.Where(item => item.Text.Contains(SearchTxtBox.Text));
listView2.Items.Clear();
listView2.Items.AddRange(matches.ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With