Highlight search string with special characters

noccy

Member
Joined
Jan 17, 2005
Messages
13
Hi!

I am trying to highlight the search query in a gridview like this:

Code:
foo is the gridview
strQuery is the search string

        
        Dim itm As Object
        Dim cel As TableCell
        Dim iHit As Integer
        Dim strOriginal As String
        Dim strStart As String
       
        If Len(strQuery) > 0 Then
        
            For Each itm In foo.Rows
                      
                For Each cel In itm.Cells
                    
                                   
                    If cel.Text.ToLower.Contains(strQuery.ToLower) And cel.Text <> "&nbsp;" Then
                        
                                              
                        strStart = ""
                        strOriginal = cel.Text
                    
                        iHit = InStr(strOriginal.ToLower, strQuery.ToLower)
                    
                                           
                        If iHit > 1 Then
                                                
                            strStart = Left(strOriginal, iHit - 1)
                        
                        End If
                        
                        cel.Text = strStart & "<span style=color:red;background-color:Lime;font-weight:bold>" & Mid(strOriginal, iHit, Len(strQuery)) & ""
                          
                          cel.Text = cel.Text & Right(strOriginal, Len(strOriginal) - (iHit + Len(strQuery)) + 1)                                     
                     
                    End If
                    
                Next
           
                Next
        
        End If

This works like charm, but if the search string contains any special charachters like the norwegian
 
special char to lower

What do the special chars equal when converted to lower? Meaning, what is the toupper and the tolower of the
 
If you are using .Net then try the string methods for comparisons etc. rather than the VB style ones.
The .Net 2 methods allow you to specify the compare options you need i.e.
Code:
  ihit = strOriginal.IndexOf(strQuery, StringComparison.CurrentCultureIgnoreCase)
this will prevent errors where upper and lower case cannot simply be converted and should be more reliable.
 
Back
Top