Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable to Listview

Tags:

c#

.net

winforms

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

like image 956
MediSoft Avatar asked Dec 20 '25 03:12

MediSoft


1 Answers

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());
like image 177
Hans Passant Avatar answered Dec 21 '25 16:12

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!