Reputation: 127
I want to search an existing ListView that has FOUR columns and print the results of the search in an entirely new ListView. Here is what I have thus far:
private void qryBtn_Click(object sender, EventArgs e)
{
var results = listResx.Items.Cast<ListViewItem>()
.Where(x => x.Text.Contains(textQuery.Text));
foreach (var d in results)
{
listQuery.Items.Add(d.ToString());
}
}
Upvotes: 0
Views: 1639
Reputation: 55750
If you're trying to search in all four columns of each item than something like this should to the trick:
var results = listResx.Items.Cast<ListViewItem>()
.Where(x => x.Subitems.Cast<ListViewItem.ListViewSubItem>()
.Any(si => si.Text.Contains(textQuery.Text)
);
results
would then contain ListViewItems where the query text matched any of the sub-items. And as far as I recall, the sub-items's collection includes the item's text as well.
To insert those as full list items in the other list view you will have to create a new ListViewItem for each item in the results enumeration because you can't insert the same ListViewItem in two different list views at the same time:
foreach (var d in results)
{
ListViewItem lvi = new ListViewItem();
foreach(ListViewItem.ListViewSubItem si in d.SubItems)
{
lvi.SubItems.Add(si.Text);
}
listQuery.Items.Add(lvi);
}
Upvotes: 1