Searching for items in multi column listview in c#

6.1k views Asked by At

Actually I am creating a playlist view in C#, the Form contains a ListView with 4 columns "Name", "Album", "Artist" and "Path" respectively. I also have a textbox in the Form. I want to know that how can I search for the items matching the user's search query and find the good possible matching results from all of the columns. For example if a user types the path then how can I get the matching values form path, and if the path contains a name of song then? Actually if you have used any player with playlist like Windows Media Player, when we type in the search query then it shows the possible result. So how can I search for results from all 4 columns. The code currently I am using is only able to search in 1'st column i.e. "Name" column.

Can I replace the textbox with combo box, in which a user can type the search query and maximum of 15 results will be added and shown to the droplist of it. When a user select any item from the dropbox then it should return the path, and the index of that item from listview.

Thank you.

1

There are 1 answers

0
evandertino On

This is an updated answer to the above solution

  foreach (ListViewItem item in listView1.Items)
    {
        if (item.Text == "searchTerm")
        {
            // do something
        }

        foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
        {
            if (subItem.Text == "searchTerm")
            {
                // do something
            }
        }
    }