0

There are lots of questions around this but none solve my problem. I have a SQL server database as the datasource, an input text box and a search button. When the text is entered and the search button is pressed, a dropdown list of rows that contain the searched text is displayed. The user selects the row they want to look at and that information is displayed in a gridview. (1 row returned)

I want the searched text to be highlighted. This is what I have and it should work but I cant figure out why it doesn't:

foreach (GridViewRow row in searchTextGridView2.Rows)
        {
            string text = searchText_txt.Text; //Text that was entered in the search text field
            int length = searchTextGridView2.Columns.Count; //Number of Columns on the grid
            for (int i = 0; i < length; i++) //loop through each column
            {
                string newText = row.Cells[i].Text.ToString(); //Get the text in the cell
                if (newText.Contains(text)) //If the cell text contains the search text then do this
                {
                    string highlight = "<span style='background-color:yellow'>" + text + "</span>";
                    string replacedText = Regex.Replace(newText, text, highlight, RegexOptions.IgnoreCase);
                    row.Cells[i].Text = replacedText;
                }
            }
        }

The above code is inside the event for the dropdown selected item changed. If I searched for "claims", it will highlight all instances of that word but if I searched for "Claims", it only highlights words with the capital "C". Any help appreciated

3
  • I'd like to point out that, even after fixing the case-sensitive Contains issue, your code will still (1) replace instances of x with X if you search for X, and (2) replace everything with . if you search for .. Commented Oct 2, 2014 at 9:36
  • Yeah just running into that now. I'm thinking I'll have to get the length of the search item, find the index in the cell then use those variables to copy the text that was found in the cell to a new variable so that the case is maintained Commented Oct 2, 2014 at 9:43
  • 1
    For (1), if you switch "<span style='background-color:yellow'>" + text + "</span>" to "<span style='background-color:yellow'>$0</span>", the $0 tells the regular expression to use the text it matched in the replacement. For (2), you can use `Regex.Escape1 on the input. Commented Oct 2, 2014 at 9:52

1 Answer 1

4

Your problem is not from the Replace() method - it's the Contains() method.

Whenever you call Contains() on a string it will perform case-sensitive comparison so the following line will always return false:

"Some Claims".Contains("claims");

In order to overcome this you should use String.IndexOf(String, Int32) method:

for (int i = 0; i < length; i++) 
{
    string newText = row.Cells[i].Text.ToString(); 
    if (newText.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0
    {
        string highlight = "<span style='background-color:yellow'>$0</span>";
        string replacedText = Regex.Replace(newText, text, highlight, RegexOptions.IgnoreCase);
        row.Cells[i].Text = replacedText;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This sounds like it should work but it didnt:( It did point me in the right direction though with the .Contains. I used the int index = newText.IndexOf(text, StringComparison.CurrentCultureIgnoreCase); instead and that works great.. ty
@KieranQuinn, my bad; I did had in mind to point to that specific overload but completely forgot. I have edited the answer.
@Rawling, are you sure? MSDN doesn't have such overload mentioned nor does the Visual Studio intellisense.
@RePierre My apologies, I'm talking absolute rubbish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.