Typically I store the data outside of the ListView, and search on that, but you can search through a listview.
I am not sure what exactly you want to do. I most comonly search ListViews for Identities (or a unique value, integer or string, that indetifies the record and makes it unique). Normally I store the unique identifiers in the Tag Field of a row in a list view
'This is how I add it
Dim xItem as New ListViewItem("FirstName")
xItem.subItems.add("LastName")
xItem.subItems.add("FamilyName")
xItem.subItems.add("PetName")
xItem.Tag = UniqueKey 'This could be the primary key from the Database
ListView1.Items.Add(xItem)
'This is how I search on the tag
For r as integer = 0 to ListView1.Items.Count - 1
If ListView1.Items(r).Tag = SearchTerm Then
MsgBox("Found it on row: " & r)
Exit For 'This escapes the loop
' If I only need to find one, I don't want to continue searching
' In other words, Unique means 1 is only found*
End If
Next r
'this searches all columns of each item
'and sends a message box when it finds them
Dim SearchTerm as String
For r as integer = 0 to ListView1.Items.Count - 1
For c as integer = 0 to Listview1.Items(r).SubItems.Count - 1
If ListView1.Items(r).SubItems(c).Text = SearchTerm
MsgBox("Found one on row: " & r & " column: " & c)
End If
Next c
Next r
'Lets say I want to search for multiple records, and return that in an array.
'Since a 2 dimensioned array is a pain, I will use the Point object
'which holds an X (for Column) and Y (for Row)
Public Function GetSearchResultsInArray(ByVal SearchTerm as String) as Point()
Dim P() as Point()
'This is tricky, we don't know how many results there will be
'So we are going to have to Redimension the bounds of this array
'during run time. We will use ReDim for that, and ReDim Preserve
'To know when to do which, we need to check if any values exist in P() yet
'When No Bounds have been Dimensioned on an Array, it will cause an Exception (slips my mind which, but probably a NullReferenceError)
'So we will check and see if it 'Is Nothing'.
For r as integer = 0 to ListView1.Items.Count - 1
For c as integer = 0 to ListView1.Items(r).SubItems.Count - 1
If ListView1.Items(r).SubItems(c).Text = SearchTerm Then
'Check the state of P()
If isNothing(P) = True Then
ReDim P(0) 'ReDim makes the object lose all of it's content
ElseIf
ReDim Preserve P(P.Length) 'The Preserve keyword makes P keep it's contents
End If
' P.Length - 1 is the uppermost bound of the array, the one we just made
P(P.Length - 1) = New Point(C, R)
End If
Next c
Next r
Return P
'This has a case at returning a Null P() object, make sure you check
'Example of how in next function
End Function
Public Sub TestIt()
Dim PointArray() as Point
PointArray = GetSearchResultsInArray("TestTerm")
If IsNothing(PointArray) = True Then
MsgBox("No Results")
Else
For i as integer = 0 to PointArray.Length - 1
MsgBox("Column: " & PointArray(i).X & ", row: " & PointArray(i).Y)
Next i
End If
End Sub
hope that helps
FireSickle.Com