Link to home
Start Free TrialLog in
Avatar of Rowel Virgo
Rowel VirgoFlag for Philippines

asked on

Display the items searched in DatagridView (Textbox, Button and Datagridview)

Hi,
I'm using Visual Studio Community 2017. In my case, I have text box named: txtSEARCH, Button named: cmdSEARCH and a Datagridview named: DataGridView1.

Datagridview populated from a text file named: "D:\esdcheck\.BAK.dll" and manualy put column in properties.
User generated imageand this is my DataGridView (This is my form)
User generated image
What I want to do is to search User ID and display it in DataGridView

ex:
I type "1504579" in the text box (txtSEARCH) and when I click the button, datagridview will display all "1504579" in the list 'ONLY' from Rowel Virgo

I'm searching all day in the internet but no codes for like that, any help please and Thank you

This is How I populate my DatagridView:

Public Class Form1


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim lines = (From line In IO.File.ReadAllLines("D:esdcheck\BAK.dll")
                     Select line.Split(CChar(","c))).ToArray
        For x As Integer = 0 To lines.GetUpperBound(0)
            DataGridView1.Rows.Add(lines(x))
        Next

    End Sub

End Class

Open in new window


this is my first post Listview and Datagridview  and thank you for the reply sir
Scott McDaniel (Microsoft Access MVP - EE MVE )
Account
Ryan Chong
Account
Avatar of HainKurt
HainKurt
Flag of Canada image

just create a dynamic filter and apply it to datasorce.defaultview,filter

like

"[UserName] + [User ID] + [Department] like '*" & mySearchText & "*'"
or
"[UserName] + [User ID] + [Department] like '%" & mySearchText & "%'"

just a few lines of code, and you should be ok...
then set filter to "" to show all rows again...
Avatar of Rowel Virgo

ASKER

Yey! I got the code BUT!

when I click the cmdSEARCH button, it display this error, any one please help
error:
System.Windows.Forms.DataGridViewCell.Value.get returned Nothing.

I got the code here:
Search Inside Datagridview Winforms VB.NET
Private Sub cmdSEARCH_Click(sender As Object, e As EventArgs) Handles cmdSEARCH.Click

        If txtSEARCH.Text <> "" Then
            Dim s As String = txtSEARCH.Text.Trim
            DataGridView1.ClearSelection()
            DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            Dim someText As String = s
            Dim gridRow As Integer = 0
            Dim gridColumn As Integer = 0
            For Each Row As DataGridViewRow In DataGridView1.Rows
                For Each column As DataGridViewColumn In DataGridView1.Columns
                    Dim cell As DataGridViewCell = (DataGridView1.Rows(gridRow).Cells(gridColumn))
                    If cell.Value.ToString.ToLower.Contains(someText.ToLower) Then
                        'cell.Style.BackColor = Color.Yellow 
                        DataGridView1.FirstDisplayedScrollingRowIndex = Row.Index
                        DataGridView1.Item(cell.ColumnIndex, cell.RowIndex).Selected = True

                    End If
                    gridColumn += 1
                Next column
                gridColumn = 0
                gridRow += 1
            Next Row
        Else
            DataGridView1.ClearSelection()
        End If

    End Sub

Open in new window

Sorry.JPG
@Repost

I put the code in Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
but it only highlights the rows with the same text i inputed, how to display only the text I searched?

I searched for Names but this happend:
User generated image
I searched for User ID but this happened:
User generated image
DataGridView1.Item(cell.ColumnIndex, cell.RowIndex).Selected = True

Open in new window


dont use this

set rowselection of grid to false, and only select cells...
From HainKurt Reply
I can't sort the searched character
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks sir