Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi!

 

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

 



'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 <> " " 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)) & "</span>"
                         
                         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 æ, ø and å, it wont work....

 

Any ideas?

 

 

noccy

Posted

special char to lower

 

What do the special chars equal when converted to lower? Meaning, what is the toupper and the tolower of the æ, ø and å?

 

Just a guess but since it works w/o those characters, that's probably where the error's at, and it never finds it's tolower version in the table, so you're going to have to find a way to handle those.

 

I haven't messed with this so there's probably a better way, but I'm pretty sure this would work:

Look at each char in the querystring:

if it's a standard, convert.tolower then append to a stringbuilder

if it's not standard, just append to the stringbuilder.

When you've finished converting the query into the stringbuilder, then use the stringbuilder.tostring to compare to your cells.

  • Administrators
Posted

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.

 ihit = strOriginal.IndexOf(strQuery, StringComparison.CurrentCultureIgnoreCase)

this will prevent errors where upper and lower case cannot simply be converted and should be more reliable.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Yes, it seems to be a problem with upper and lower... "Å" ToLower returns "Å"... I will try your suggestions as soon as I am back at work...

 

Thanks :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...