0

I have a filter by text search in my wpf application. However when I do a string comparison to check if it contains a buzzword like, oh say "error", I want it to update/refresh my datagrid with all entries that have the Error keyword; regardless if I typed Error, or error, or eRRor in my search box.

Here is my code:

public class Foo 
{
  private void GetFilteredResults(MessageDetails detail, FilterEventArgs e)
  {
    foreach (MessageValue value in detail.MessageValue)
    {
       if (value.Value.Contains(txtFilterValue.Text))
       {
           //Returns true...
       }
    }
    //Otherwise false
  }
}

The Messagedetails is a container class and holds all of the datagrid row values.

The MessageValue is a struct that holds the actual message value in an ObservableCollection

Finally, the txtFilterValue is the control name of the textbox I am using to for my word filter

What I want to do is setup something to where I remove case sensitivity in order to cache all entries that match my keyword, regardless of how I type it. How would I go about that?

mmangual83
  • 151
  • 1
  • 2
  • 11
  • `txtFilterValue.Text.ToUpper()` or `txtFilterValue.Text.ToLower()` to convert the string to only uppercase or lowercase – Matt L. Aug 22 '17 at 20:20

1 Answers1

1

Let's say there is a boolean property CaseSensitive identifying the search mode. Then you can use string.IndexOf to solve this by setting the StringComparison correctly:

StringComparison comparison = CaseSensitive ?
    StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
if (value.Value.IndexOf(txtFilterValue.Text, comparison) >= 0)
{
    //Returns true...
}

The whole query can be simply written with LINQ like

private void GetFilteredResults(MessageDetails detail, FilterEventArgs e)
{
    bool comparison = CaseSensitive ?
        StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
    return detail.MessageValue.Any(v => v.Value.IndexOf(txtFilterValue.Text, comparison) >= 0);
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49