Trying to search ListView for subitems matching a string

c#, listview, winforms

Solution

You can use the FindItemWithText method.

ListViewItem searchItem = null;
int index = 0;
do
{
    if (index < Program.booker.listView.Items.Count)
    {
        //true = search subitems
        //last false param = no partial matches (remove if you want partial matches)
        searchItem = Program.booker.listView.FindItemWithText(date, true, index, false);
        if (searchItem != null)
        {
            index = searchItem.Index + 1;

             //rest of code
        }
    }
    else
        searchItem =null;

} while (searchItem != null);

Problem

I'm having trouble scanning through a ListView to locate a subitem matching a given string. Here's my code: ``` private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { string date = datePicker.Value.ToShortDateString(); int count = Program.booker.listView.Items.Count; for (int i = 0; i < count; i++) { ListViewItem lvi = Program.booker.listView.Items[i]; if (lvi.SubItems.Equals(date)) { MessageBox.Show("Found!", "Alert"); Program.booker.listView.MultiSelect = true; Program.booker.listView.Items[i].Selected = true; } else { MessageBox.Show("Nothing found for " + date, "Alert"); } } } ``` The ListView is located on the Booker form, and I'm accessing it from the Filter class. I'd like to search the entire ListView for any items matching my date string. Thanks!

Original source