• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 58
  • Last Modified:

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
        Else
            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
            Next
            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
            dgrow.Cells.Add(dgcell)
            dgvorders.Rows.Add(dgrow)
        Next

    End Sub
0
Seamus2626
Asked:
Seamus2626
  • 5
  • 2
1 Solution
 
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.
0
 
Seamus2626Author Commented:
If line Like "*" & txtcustomername.Text & "*"  wasn't working. That worked perfect. Thanks very much
1
 
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.
28944619_1914856758844692_1244554001.jpg
28944259_1914857665511268_2029949411.jpg
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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

        Try
            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



        Finally
            If Not reader Is Nothing Then
                reader.Close()
            End If
        End Try

    End Sub







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

    End Sub
End Class
0
 
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.

eg:

firstname,lastname, employeeid
0
 
Seamus2626Author Commented:
Sorry. Didn't realise the practice. Thanks very much.
0
 
Seamus2626Author Commented:
Solved. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now