vb.net Search for Full or partial match in Datagridview from TextBox and select the first match while displaying the full datagrid
<p>I have a customers table that is displayed in a datagridview.  I would like the user to be able to enter the customers full or partial last name and click a buttom that would then find the first customer that met the match in the text box.  As an example: The user types "wil" into the text box and the first record found is for "williams" even though the user is looking for "wilson".  The record would be highlighted(selected) and the user could scroll to look at other records and select "wilson" manually (the manual part I can code). </p>

<p>I have searched for hours on the internet and cannot find this type of code.  Most of it is filtering or searching every cell and returning every value. </p>

<p>I am currently reworking a project I did with an access database and vba several years ago. I had thought vb.net would be very similar but it is not similar enough for me to modify this code.  I'm also going to use a sql database.</p>

<p>The index field is obviously cell(0) and last name is cell(1).</p>

<p>I have found a solution although I did have to modify it.  It will do everything I need except one thing.  If I type the letter "H" and do a search on last name, it finds the first lastname that has an "H" in the last name but in a different position from the first letter.  I need it to go to the first last name that begins with an "H".  I have listed my code below.</p>

<p>Dim srch As String
        Dim irowindex As Integer
        Dim strl As Integer
        srch = txtSearch.Text</p>

<pre><code>    dgvCustomers.ClearSelection()
    For i As Integer = 0 To dgvCustomers.Rows.Count - 1
        If dgvCustomers.Rows(i).Cells(0).Value IsNot Nothing Then
            If dgvCustomers.Rows(i).Cells(1).Value.ToString.ToUpper.Contains(srch.ToUpper) Then
                dgvCustomers.Rows(i).Selected = True
                dgvCustomers.RowsDefaultCellStyle.SelectionBackColor = Color.DimGray
                irowindex = dgvCustomers.SelectedCells.Item(0).Value
                MessageBox.Show(irowindex)
                Exit For
            End If
        End If
    Next
End Sub
</code></pre>