Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a DataGridView

Status
Not open for further replies.

bobbyforhire

Technical User
Mar 11, 2008
253
0
0
US
I have a self populated datagridview getting it's data from a csv. I am trying to add a search field to find text in a coloum and then goto the colum then goto the next item if any. So far i've been able to search for the item and it finds it and goes to it, but i don't know how to tell it to goto the next item if any insead of just re-finding the first item.

here is my current code for the search.

For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For j As Integer = 0 To Me.DataGridView1.Rows(i).Cells.Count - 1
If Me.DataGridView1.Item(j, i).Value IsNot Nothing Then
If Me.DataGridView1.Item(j, i).Value.ToString.Contains(TextBox1.Text) Then
' CType(Me.DataGridView1.Item(j, i), DataGridViewCell).Style.BackColor = Color.Blue
Me.DataGridView1.ClearSelection()
Me.DataGridView1.CurrentCell = Me.DataGridView1.Item(j, i)
End If
End If
Next
Next
 
I would build up a hashtable or something to contain the matches found. Instead of setting the current cell in your for loop, add to your collection of matches.

Then, in your "go next" button click event (or whatever you are providing to the user to go next), you can loop through your collection and set the current cell.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top