Hi everyone. I'm new here, and would love you to help me with a little task...

basicly i've got ListView control on the form, and TextBox. I'd like to have kind of... search system that would do folowing:
when i start typing in TextBox (KeyDown event) all my ListView will be cleared and only items that have "StartsWith" whatever i type are listed in the ListView... I hope i've explained clearly Smile i've seen this in many programs... It's something like "Google Sugest" thing, but works with ListView...

Code that i have now is this:

Code Block

private void textbox_search_KeyDown(object sender, KeyEventArgs e)
string SearchPattern = MyTextBox.Text;

foreach (ListViewItem LVI in MyListView.Items)
if (LVI.Text.StartsWith(SearchPattern))
LVI.Selected = true; // just to test...


the data received from remote server (mysql)

Code Block

if (OdbcCon.State == ConnectionState.Open)
OdbcCom = new System.Data.Odbc.OdbcCommand("SELECT * FROM mytable", OdbcCon);
OdbcDR = OdbcCom.ExecuteReader();

while (OdbcDR.Read())
// Create some listview items consisting of first and last names.
listviewitem = new ListViewItem("" + OdbcDR[1] + "");
listviewitem.SubItems.Add("" + OdbcDR[2] + "");
listviewitem.SubItems.Add("" + OdbcDR[3] + "");
listviewitem.SubItems.Add("" + OdbcDR[4] + "");
listviewitem.SubItems.Add("" + OdbcDR[5] + "");
listviewitem.SubItems.Add("" + OdbcDR[6] + "");
listviewitem.SubItems.Add("" + OdbcDR[7] + "");

thank you

Re: Windows Forms General How to search through ListView control?

Derek Smyth

Hi, welcome to the forums

Here is some information on how to search for items in a list view...

The FindItemWithText() method allows you to perform a text search on a ListView in list or details view, given a search string and an optional starting and ending index.

ListViewItem found = aListView.FindItemWithText("brack")

if (found == null) ...

However I don't think this is what your looking for. What your really looking to do is filter the source data and then populate the listview from the filtered data. The search changes you again filter your data based on the search criteria and then populate the listview from the filtered data.

Since your using a database then I'd recommend either running a query against the database and getting a filtered set of data and then displaying it the same way you are now. This will be the easiest option, wouldn't require a big code change but would cause a lot of calls on the database, esp if your doing per key stroke.

The other option is you load all your data into a DataTable object (a database table) and then filter the data in there using a DataView class (wraps around the table and lets you get different views of it). If you go this way then it might be an option to, instead of using the ListView control, use the DataGridView control which is more geared towards viewing database information. The advantage is if you bound the DataGridView to the DataView then when you set the DataViews filter the DataGridView will automatically update.

This could be a better alternative that will look different but will essentially do the same thing. Obviously this is a completely different solution to what you have now so you might not want to do it. But you really want to filter the data and then base the display on the filtered data.