Hi,

I'm a beginner in vb.net and I have a problem. At first I want to say sorry for my language, English is not my first language. Hope u onderstand my prob.

So, I'm working disconected, I have a datagrid and now I want to search in this datagrid by using the textbox that appears automatically above. When you start typing for example the d, it has to search all the familynames starting with the d, like in a phonebook on a cell phone, you get al the contacts starting with that letter.

I hope you understand my problem and that you can help me.

with best regards
Hnna

Just in case that thread doesn't help, here's some code that might. It uses a datatable as datasource. I load it with a .csv file but that's in a separate function that you can code as you wish. I make the changes to the datatable that are reflected in the datagridview. I use the TextChanged event of the textbox to change which rows are displayed. As you type the selection keeps changing according to what you're typing. Change the value of SearchColumn to search by a different column. As an added bonus when the number of rows is whittled down to 1 the text in the textbox is replaced with the value of the SearchColumn in that row. Some of this might not be very efficient, but that shouldn't matter unless you're using a huge amount of data. Bottom line it works:

Form controls

        '
        'DataGridView1
        '
        Name = "DataGridView1"
        '
        'TextBox1
        '
        Name = "TextBox1"

Form code

Imports System.IO
Public Class Form1
    Dim dtMain As New DataTable
    Dim SearchColumn As Integer = 0
    Dim FilePath As String = Application.StartupPath + "\lgbackup.csv"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetData(dtMain)
        DataGridView1.DataSource = dtMain
    End Sub
    Private Sub GetData(ByRef dt As DataTable, Optional ByVal Header As Boolean = True)
        Dim Fields() As String
        Dim Start As Integer = CInt(Header) * -1
        If Not File.Exists(FilePath) Then
            Return
        End If
        dt.Clear()
        Dim Lines() As String = File.ReadAllLines(FilePath)
        If CBool(Start) AndAlso dt.Columns.Count = 0 Then
            Lines(0) = Lines(0).Replace(Chr(34), "")
            For Each h As String In Lines(0).Split(",")
                dt.Columns.Add(h)
            Next
        End If
        For I = Start To Lines.Count - 1
            Lines(I) = Lines(I).Replace(Chr(34), "")
            Fields = Lines(I).Split(",")
            dt.Rows.Add(Fields)
        Next
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        DoSearch(SearchColumn)
    End Sub

    Private Sub DoSearch(ByVal sc As Integer)
        GetData(dtMain)
        Dim DeleteRowList As New List(Of DataRow)
        For Each row As DataRow In dtMain.Rows
            Dim TempStr As String = row.Item(sc).ToString
            If Not TempStr.ToUpper.StartsWith(TextBox1.Text.ToUpper) Then
                DeleteRowList.Add(row)
            End If
        Next
        For Each row As DataRow In DeleteRowList
            dtMain.Rows.Remove(row)
        Next
        If dtMain.Rows.Count = 1 Then
            TextBox1.Text = dtMain.Rows(0).Item(sc).ToString
            TextBox1.SelectionStart = TextBox1.Text.Length
        End If
    End Sub
End Class

tinstaafl, thanks a lot for that piece of code. I've been searching on how to filter a datagridview for quite a while with no luck until now.

I have a small issue I can't seem to figure out. I populated my datagridview using a linq query and set the columns header text to something specific in the sub that displays the data(through a button click event) from the csv file. After inserting your code it seems to use the first line of the csv file as the header text, how would I set the header text using your code? I'm fairly new at programming using vb and don't quite understand all of your code and what a few things are doing so I'm not sure what to edit or delete from it.

Thanks in advance.

This is the code I used to populate my datagridview in case it helps:

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim mediaData() As String = IO.File.ReadAllLines(mediaCollection)

        If IO.File.Exists(mediaCollection) Then
            Dim query = From record In mediaData
                        Let data = record.Split(","c)
                        Let mediaName = data(0)
                        Let mediaTitle = data(1)
                        Let mediaType = data(2)
                        Select mediaName, mediaTitle, mediaType
            dgvOutput.DataSource = query.ToList
            dgvOutput.CurrentCell = Nothing
            dgvOutput.Columns("mediaName").HeaderText = "Main Actor/Performer"
            dgvOutput.Columns("mediaTitle").HeaderText = "CD/DVD Title"
            dgvOutput.Columns("mediaType").HeaderText = "CD or DVD"
        Else
            Return
        End If
    End Sub

mediaCollection is declared outside of the btnDisplay sub as a string which stores the path to the csv file.

Inserting your code did work perfectly just editing a few variables to match what I had already declared, the only issue is the column header text. I'm thinking of just making the first line in my csv file the names I want for each column but if there's another easy solution or a way to use the linq query I already have to achieve the same type of filter I'd love to know.

When you call the getdata routine make add the header parameter and make it false, GetData(dtMain, False). that will load the data without a header line. One thing I didn't put code in there for, is to check if the columns are already added to the table. With the header parameter set to false, if there are no columns it will throw an exception. Therefore you must make sure the columns are added to the table before you call getdata.

In your case, if you're using a datatable like my example, I would suggest setting the columns in the datatable, instead of in the DGV directly. Then call the getdata sub routine with the header parameter set to false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.