IEnumerable for Listview

I want to write a method to filter the browse list

IEnumerable<ListViewItem> CurrentList;
CurrentList = ListViewExemple.Items.Cast<ListViewItem>(); 
var result = CurrentList.Select(i => i.Text.Contains(SearchTxtBox.Text));

      

Now How can I add result items to the cleaned up Listview

+3


source to share


2 answers


You need to keep in mind that the ListViewItem has an owner, you need to make a copy of the item in order to put it in another list. Ease of use of the Clone () method. Or move it from one to the other, unlikely 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());

      

+2


source


Unfortunately, it ListView

does not have the ability to bind data. Therefore, you need to add items manually:

foreach (var item in result)
    someListView.Items.Add(item);

      

Or perhaps:

someListView.Items.AddRange(result);

      



Also, you seem to have an error:

CurrentList.Select(i => i.Text.Contains(SearchTxtBox.Text))

      

This will not create collection ListViewItem

s, and it will create collection bool

s. I think you meant this:

CurrentList.Where(i => i.Text.Contains(SearchTxtBox.Text))

      

0


source







All Articles