Search textbox with datagrid

Hi. I am trying to search a datagridview using a textbox. As I type into the textbox, I want it to gradually generate the word. Thanks in advance, Here is my code so far

Dim lines() As String = My.Computer.FileSystem.ReadAllText("C:\Users\Sean Travers\Downloads\clubituplesswasteofmoney1\clubituplesswasteofmoney\orderdata.txt").Replace(vbLf, "").Split(vbCr)
    Private Sub txtcustomername_TextChanged(sender As Object, e As EventArgs) Handles txtcustomername.TextChanged
        Dim searchedlines(-1) As String 'create an array to fill with terms that match our search

        If txtcustomername.Text = Nothing Then
            datapopulate(lines) 'just populate the datagridview with our text file array
            For Each line In lines
                If line Like "*" & txtcustomername.Text & "*" Then 'check if anything in our search matches any of our terms
                    ReDim Preserve searchedlines(UBound(searchedlines) + 1) 'resize the array to fit our needs
                    searchedlines(UBound(searchedlines)) = line 'add the matched line to our array
                End If
            datapopulate(searchedlines) 'populate the datagrid with our matched terms array
        End If
    End Sub
    Private Sub datapopulate(ByVal mylist)
        Dim dgrow As DataGridViewRow
        Dim dgcell As DataGridViewCell

        dgvorders.Rows.Clear() 'clear the grid

        For Each line As String In mylist 'do the same thing here that we did on form load
            dgrow = New DataGridViewRow
            dgcell = New DataGridViewTextBoxCell
            dgcell.Value = line

    End Sub
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
 If line Like "*" & txtcustomername.Text & "*"  

Does that work for you?

If not then you could to if line.Contains(txtCustomerName.Text)

Also consider redimming to twice the size of the current list.  A redim can be very expensive as you're essentially creating a new array in memory and then copying all the elements over.

Start an index at 0, every time you add a line, index + 1

if Index = ubound (searchedlines) - 1 then
   ReDim Preserve searchedlines(UBound(searchedlines) * 2)
end if

Overall looks okay though.
Seamus2626Author Commented:
If line Like "*" & txtcustomername.Text & "*"  wasn't working. That worked perfect. Thanks very much
Seamus2626Author Commented:
I am also trying to add columns to a gridview and transfer data from a textfile to those colums on another form. When I add the third column "EmployeeNumber" it is displaying the wrong information. Here is a picture of how it's displayed and a picture of the textfile.  Any help would be much appreciated. Thanks.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Seamus2626Author Commented:
This is the code I am using.

Public Class manageradministration
    Private Sub dgvemployees_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvemployees.CellContentClick

    End Sub
    Private FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "managerfile.txt")
    Private Sub manageradministration_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        dgvemployees.DataSource = (From linee In IO.File.ReadAllLines("E:\New folder\managerfile.txt")
                                   Where linee.Length > 0 Let Items = linee.Split(","c)
                                   Select New With {.FirstName = Items(0), .LastName = Items(1), .EmployeeNumber = Items(1)}).ToList
        If dgvemployees.Rows.Count = 1 Then
            ' Decide on what to do for one last line/row
            Exit Sub
        End If

        Dim sb As New System.Text.StringBuilder

        Dim line As String
        Dim reader As IO.StreamReader = Nothing

            reader = New IO.StreamReader("E:\New folder\managerfile.txt")

            line = reader.ReadLine

            While Not line Is Nothing
                Dim linese As String() = line.Split(New Char() {"#"c})
                Dim parts As String() = line.Split(",")

                line = reader.ReadLine
            End While

            If Not reader Is Nothing Then
            End If
        End Try

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub
End Class
Kyle AbrahamsSenior .Net DeveloperCommented:
That would be a separate question . . . usual practice is to close this one and open a new one so people who have similar issues can use the solutions found here and apply it to their problems.

With that said you're most likely not parsing the data correctly.  It would be better to add the ID as a 3rd column in your CSV file.


firstname,lastname, employeeid

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seamus2626Author Commented:
Sorry. Didn't realise the practice. Thanks very much.
Seamus2626Author Commented:
Solved. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.