VB.NET 2008 Express, ACCESS Db 2007

Ok I have a program that contains a datagridview control linked to a database and i want to be able to search through every field via a ButtonSearch for a match of the search term - search term is entered in a text box labeled search - and display only the records that have a match in the datagridview.

Thanks!

Recommended Answers

All 4 Replies

Just semantics/terminology but something to keep in mind, a datagridcontrol doesnt actually hold the data or connect directly to a database, you assign it a datasource such as a dataset/datatable etc... The underlying datasource is what you want to search & filter.

To answer your actual question, take a look at "DataView.RowFilter" in the help index. It will show you how to filter the data in your dataset/datatable and has an example. If you still need help just let me know.

Thanks for the help, but the help only seems to describe an example where the field to be searched is specified.
In my case, I need to be able to search through every field on the form for a term as it may be in any field.
I can successfully filter the datagridview using SQL and searching all records by one field, I want to search all records by all fields.. sorry hard to explain..

My query was

UpdateGrid("SELECT * FROM TrackTable WHERE * = '" & txtSearch.Text & "' ORDER BY ArtistName ASC")

Unfortunately, VB doesn't accept the second "*" .. says a parameter is missing. I replaced it with a list of all column names, separated by "OR". This didn't show up any errors when debugging, but it seemed to be doing absolutely nothing on the form. nothing happened.

You have to specify every single field that you want to search. You also have to specify if your looking for an exact match or perhaps one word (or even part of a word) within an individual field. For instance

Select *
From myTable
Where Column1 Like '%mySearchText%'
    Or Column2 Like '%mySearchText%'

The percent sign is a wild card telling it I dont care what letters/words are before or after my search text within that field. For exact matches you can simply use the equals sign followed by your search word(s).

Obviously that is if your are filtering during your actual query. To filter an already filled datatable, the method I mentioned earlier still applies. And yes you can add more then one column to be searched with that method. However searching every field is very process intensive I would suggest letting your database do that type of filter.

Thank you so much the code you gave me worked fine .. i just swapped the "%" with "&".

It was all a matter of where to put the "OR".

I didn't use the RowFilter in the end but thanks anyway Tom.

Mission accomplished.

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.