Perform search on Datagridview using SQL queries


New member
Mar 14, 2020
Programming Experience
I've written some code to import an Excel spreadsheet into DataGridView.

I now need to write some code to perform a search on the DataGridView, where the user enters some search criteria into a text box, and then DataGridVIew control fills with matching results.

So, I looked online for a solution, and was pleased to see that quite a lot of people had done the same thing that I required. The code would work fine, and I know a bit of SQL from work, however those people are referencing Access databases in their code and establishing connections to databases, not Excel spreadsheets.

Looking around I've found some code that will find matching results, and select them, but it only finds the first result, and it doesn't clear the DataGridView.

Dim str As String = tbx_SearchCriteria.Text
If Me.tbx_SearchCriteria.Text.Trim(" ") = " " Then
For i As Integer = 0 To DGV1.Rows.Count - 1
For j As Integer = 0 To Me.DGV1.Rows(i).Cells.Count - 1
If DGV1.Item(j, i).Value.ToString().ToLower.StartsWith(str.ToLower) Then
DGV1.Rows(i).Selected = True
DGV1.CurrentCell = DGV1.Rows(i).Cells(j)
Exit Try
End If
Next i
End If
Catch abc As Exception
MessageBox.Show("Not found!")
End Try

So my question(s) are:

- Am I able to perform a search on DataGridView using SQL queries if I have a spreadsheet connected, NOT a database.
- How could I change the above code to perform the search on the Excel spreadsheet?
Top Bottom