I want to filter a text in all columns in a datagridview. So I use Like % in the filter text. However, I found that the text(strFilter in my code) may contain % sometimes. So it have a syntax error. How can I use filter with like clause when the filter text contains %?
Private Sub PassFilter(ByRef dataTable As DataTable, ByVal strFilter As String)
Try
Dim dataview As DataView = dataTable.DefaultView
Dim strExp As String = ""
For Each col As DataColumn In dataTable.Columns
If Not String.IsNullOrEmpty(strFilter) Then
If strExp = "" Then
strExp += "[" & col.ColumnName & "]" & " LIKE '%" & strFilter & "%'"
Else
strExp += " OR " & "[" & col.ColumnName & "]" & " LIKE '%" & strFilter & "%'"
End If
End If
Next
dataview.RowFilter = strExp
Catch ex As Exception
ShowMessage("Unable to filter datagridview." + ex.Message)
End Try
End Sub