Filter listview based on textbox input

  • Thread starter Thread starter Zwax
  • Start date Start date
Z

Zwax

Guest
Hi

(I apologize in advance for my bad English)

I have a listview, which I fill from a csv file. The csv file is in the following format (no blanks - its only for a better view):

Header0, Header1, Header2, Header3, Header4, Header5, Header6
AATxt0, AATxt1, AATxt2, AATxt3, AATxt4, AATxt5, AATxt6
ABTxt0, ABTxt1, ABTxt2, ABTxt3, ABTxt4, ABTxt5, ABTxt6
BATxt0, BATxt1, BATxt2, BATxt3, BATxt4, BATxt5, BATxt6


I have a textbox for user input, which I want to filter the listview from.

If the user searches for: "A"

I want it to show the rows containing cells starting with A. In this example the 2 first rows.

If the user searches with "AB", only the second line should be displayed - if blank all lines should be displayed, and so on.

It must be able to do this regardless of which column the result is in (here my example is not so good ;-) ), or alternatively could search in different columns from a result of a choice the user would have to make.


How would you do it?


I have tried with something like this:

string filesearch = txtSearch.Text + "*";

foreach (ListViewItem item in listView1.Items)
{
if (item.Text == filesearch)
{
MessageBox.Show("in item");
}

foreach (ListViewItem subItem in item.SubItems)
{
if (subItem.Text == filesearch)
{
MessageBox.Show("in subItem");
}
}
}

... just to se if I could find the value, but I get an error (InvalidCastException) in this line:

foreach (ListViewItem subItem in item.SubItems)

I guess it also has to have some .StartsWith method or something.

...and thats only half the problem - I have got to make filtering too.


But anyway - Any ideas on how to filter the listview?

Continue reading...
 
Back
Top