Click here to Skip to main content
13,091,300 members (49,830 online)
Rate this:
Please Sign up or sign in to vote.
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?

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 27-Feb-13 4:37am
Updated 27-Feb-13 4:51am
BC @ CV2.6K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use the Contains() method.

If (StringToSearch.Contains(Me.TextBox1.Text)) Then
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'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.
BC @ CV 1-Mar-13 10:52am
Actually the Contains() method will return true if there is a partial match.

true: StringToSearch = "This is a string with some text in it.";
TextBox1.Text = "tex";

false: StringToSearch = "This is a string with some text in it.";
TextBox1.Text = "txt";

true: StringToSearch = "Brian Williams";
TextBox1.Text = "Wi";
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I found the solution in another forum.

for the statement:
If StringToSearch Like (Me.TextBox1.Text) Then 
'Change to the following statment
IF Instr(StringToSearch, lcase(trim(Textbox1.Text))) <> 0 Then

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100