none
Searching listview substrings RRS feed

  • Question

  •  

    Hi I'm trying in vain to figure out the code to search a listviews substrings (columns).

     

    I have a listview populated with several columns.

     

    How do I search for a string in column two ["Artist"]?

     

    I've tried the code below and a million other variations with no success.

     

    foreach (ListViewItem item2 in mediaLibrayListView.Items)

    {

    if (searchColumn2.ToLower().StartsWith(fileSearch2Tbx.ToString().ToLower()))

    {

    mediaLibrayListView.Items.Clear();

    mediaLibrayListView.Items.Add(item2);

    mediaFilesCount = mediaLibrayListView.Items.Count;

    }

    }

     

    Mike44P

    Thursday, September 18, 2008 4:13 AM

Answers

  • Write it like this:

          foreach (ListViewItem item in listView1.Items) {
            if (0 == string.Compare(item.SubItems[1].Text, textBox1.Text, true)) {
              listView1.Items.Clear();
              listView1.Items.Add(item);
              break;
            }
          }

    Thursday, September 18, 2008 12:39 PM
    Moderator
  • Don't store the list of items in the ListView, store it separately in, say, a List<>.  That will always allow you to repopulate the LV.  And make incremental search work.
    Friday, September 19, 2008 9:51 AM
    Moderator

All replies

  • Write it like this:

          foreach (ListViewItem item in listView1.Items) {
            if (0 == string.Compare(item.SubItems[1].Text, textBox1.Text, true)) {
              listView1.Items.Clear();
              listView1.Items.Add(item);
              break;
            }
          }

    Thursday, September 18, 2008 12:39 PM
    Moderator
  • Hi nobugz

    This works wonderfully thank you.

     

    I would really like to use the incremental search feature.

    And I have this working using your code syntax.

     

    foreach (ListViewItem item in mediaLibrayListView.Items)

    {

    // if (0 == string.Compare(item.SubItems[searchColumn2].Text.ToLower(), fileSearch2Tbx.Text, true))

    if (item.SubItems[searchColumn2].Text.ToLower().StartsWith(fileSearch2Tbx.Text.ToLower()))

    {

    mediaLibrayListView.Items.Clear();

    mediaLibrayListView.Items.Add(item);

    mediaFilesCount = mediaLibrayListView.Items.Count;

    }

    }

     

    The problem as you probably already know is the clear wipes out the list so you end up with only one match.

    Ideally I would like to use the incremental search to narrow the list and permit back spacing to refresh the search when characters are eliminated.

     

    Mike44P

     

    Thursday, September 18, 2008 7:42 PM
  • Don't store the list of items in the ListView, store it separately in, say, a List<>.  That will always allow you to repopulate the LV.  And make incremental search work.
    Friday, September 19, 2008 9:51 AM
    Moderator
  • I actually just got it working yesterday using a few loops and repopulating.

    The List is an excellent idea as it should speed things up.

     

    Thank you

     

    Mike44P

     

    Friday, September 19, 2008 5:45 PM