Click here to Skip to main content
15,667,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone , please someone can tell me if there is a way to filter data from datagridview using a Textbox ?

thank you.

What I have tried:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim str As String = "Data Source=;Initial Catalog=user;Persist Security Info=True;User ID=sa;Password="
        Dim con As New SqlConnection(str)
        Dim com As String = "Select DATE ,Login ,Operation ,Nbr_docs ,tps_traite ,moy_doc_hr ,moy_sec_doc ,taux_err   from AWB_CHQ_RETOUR "
        Dim Adpt As New SqlDataAdapter(com, con)
        Dim ds As New DataSet()
        Adpt.Fill(ds, "AWB_CHQ_RETOUR")
        DataGridView1.DataSource = ds.Tables(0)
    End Sub <pre>

this is the code I use to poulate the DatagridView , and I have six buttons similar to this , that populate my datagrid from other tables .
Updated 22-May-17 7:47am

Yes there is.

Research material from Google[^]

[EDIT - here is a link to the MSDN documentation Building a Drop-Down Filter List for a DataGridView Column Header Cell[^]
Share this answer
Member 13191952 22-May-17 8:03am    
Really ! ohhhh
CHill60 22-May-17 8:07am    
Yes, really. This is not a code-writing service.
The 2nd link from that list takes you to Filter Columns Value in DataGridView - Visual Basic .NET[^] which explicitly filters data from datagridview using a Textbox - which is apparently what you want to do.
Member 13191952 22-May-17 8:08am    
I already seen that but I don't want to use an sql command in the textbox , I want just to filter what is displaying in the DataGridview
Member 13191952 22-May-17 8:05am    
I already seen that but I don't want to use an sql command in the textbox , I want just to filter what is displaying in the DataGridview
CHill60 22-May-17 8:09am    
"I already seen that" - There are 173,000 results on that link!
You don't have to read them all. The 2nd link uses the .Filter method.
There's few ways to achieve that:
1) using filter on DataSet/DataTable through the Select[^] method.
Filtering and Sorting in Datasets[^]
How to: Filter and Sort Directly in Data Tables[^]

2) using Filter method[^] for BindingSource (Caroline - CHill60[^] already mentioned about that method)

3) using SqlCommand to grab filtered data from database
You have to use the same code as you use to populate data to DataGridView. The only differ is in SELECT statement.
SELECT DATE ,Login ,Operation ,Nbr_docs ,tps_traite ,moy_doc_hr ,moy_sec_doc ,taux_err
WHERE Login = 'SomeLoginHere'

4) using Linq to DataSet[^]
LINQ to DataSet Examples[^]
101 LINQ Samples in C#[^]
Creating a DataTable From a Query (LINQ to DataSet)[^]

DataTable dt = DirectCast(DataGridView1.DataSource, DataTable)
Dim qry = dt.AsEnumerable() _
  .Where(Function(x) x.Field(Of String)("Login")="SomeLoginHere")
Dim boundTable As DataTable = qry.CopyToDataTable
DataGridView1.DataSource = boundTable
Share this answer
CHill60 22-May-17 15:11pm    
Thanks for picking this up Maciej. 5'd
Maciej Los 22-May-17 15:34pm    
Thank you, Caroline.
Member 13191952 23-May-17 5:55am    
thank you for your Help , I added the Where condition in select command where My Column like Textbox2.. and it works thank you
Maciej Los 23-May-17 6:54am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900