简体   繁体   中英

Make predicate datagridview filtering case insensitive c#

I found this great post about filtering in a datagrid ( Filter a DataGrid in WPF ) and it worked fine so far. But I have a little issue about this. It's Case Sensitive. As I'm not versed with "Predicate" i need your help here.

I have my list (allProductRows) wich contains the model "ProductRows" [ID, artNr, ProductName]

ex: [2,123,"Software Alpha"]

my search-query is here:

private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    filterDataGrid(tb_searchBox.Text);
}


/// <summary>
/// Filter DataGrid to specific Term (atm Case Sensitive)
/// </summary>
/// <param name="searchTerm">SearchTerm String</param>
private void filterDataGrid(string searchTerm)
{
    var _itemSourceList = new CollectionViewSource() { Source = allProductRows };
    ICollectionView Itemlist = _itemSourceList.View;
    var myFilter = new Predicate<object>(item => ((ProductRow)item).ProductName.Contains(searchTerm));
    Itemlist.Filter = myFilter;
    dg_products.ItemsSource = Itemlist;
}

Thanks to @ Wiimax for the code above

(reason why it's in a seperate void, is due other execution from other place)

With this state, everything i enter into the Textbox, is getting filtered correctly, but it's case sensitive.

I would assume (can't test C#), that it's as simple as adding .ToLower() to both the collection item string and the search string. Here it is applied to a line in your code:

var myFilter = new Predicate<object>(item => ((ProductRow)item).ProductName.ToLower().Contains(searchTerm.ToLower()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM