Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to select a row in a datagridview based on a partial or full match from a textbox where the user enters the customer's last name. I can make it work with a full match but not a partial match. I have tried adding a wildcard onto the end of the text box and have not been able to make it work. My code shows what works with a full match and what I have tried with the code from a partial match as a comment below it. Can someone tell me what I am doing wrong?

VB
If StringToSearch Like (Me.TextBox1.Text) Then
'If stringToSearch Like " & (me.textbox1.text) & '"%"   
     Dim myCurrentCell As DataGridViewCell = gRow.Cells(1)
     Dim myCurrentPosition As DataGridViewCell = gRow.Cells(0)
     DataGridView1.CurrentCell = myCurrentCell
     CurrentRowIndex = DataGridView1.CurrentCell.RowIndex
     CurrentRowIndex = DataGridView1.CurrentRow.Index
     DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSkyBlue
     Found = True
End If
Posted
Updated 27-Feb-13 4:51am
v3

Use the Contains() method.

VB
If (StringToSearch.Contains(Me.TextBox1.Text)) Then
 
Share this answer
 
Comments
wlhj1 27-Feb-13 11:01am    
Would this still allow for the user who is searching for wilson to find the first row with wilson in the last name if the user only entered w in the textbox and some one in a preceeding row had a last name that contained w in it like waters?
BC @ CV 27-Feb-13 11:15am    
Your code sample doesn't show what StringToSearch is, only that you want to check if it contains the string in TextBox1.Text.
wlhj1 28-Feb-13 12:04pm    
Here is the code that defines StringToSearch

Dim Found As Boolean = False
Dim StringToSearch As String = ""
Dim ValueToSearchFor As String = Me.TextBox1.Text.Trim.ToLower
Dim CurrentRowIndex As Integer = 0
BC @ CV 28-Feb-13 12:19pm    
So...you're searching a blank string? O.o
I guess I don't understand what you're doing. All I can tell you is in the code I gave you the if will be true when the text in TextBox1 is found within the StringToSearch.
wlhj1 28-Feb-13 13:00pm    
I guess I didn't explain what I meant by a partial search. Your code does return an search on an exact match as does mine. However, I want to return the first match on a partial name. Ex. Find first match that starts with the letter w, find first match with the letters wi, find the first match with the letters wil, etc. (assuming that the person enters only a partial name. I'm trying to position the row selected on whatever the first match is. I am not trying to filter the data. I want the datagridview with all of its records to show. In this manner if the person searches for wilson by entering wil in the text box the search will select and highlight the first match against wil even if the match happens to be williams. Then the operator can scroll down and click on the record needed. This is because there are last names that are sometimes misspelled and/or the operator doesn't know how to spell the last name. Thanks for bearing with my poor attempt at explaining what I need.
I found the solution in another forum.

for the statement:
VB
If StringToSearch Like (Me.TextBox1.Text) Then 

'Change to the following statment

IF Instr(StringToSearch, lcase(trim(Textbox1.Text))) <> 0 Then
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900