[英]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.我发现这篇关于在数据网格中过滤的好帖子( 在 WPF 中过滤一个 DataGrid ),到目前为止它运行良好。 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]我有我的列表 (allProductRows),其中包含 model "ProductRows" [ID, artNr, ProductName]
ex: [2,123,"Software Alpha"]例如:[2,123,“软件 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;
}
(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.有了这个 state,我输入到文本框中的所有内容都会被正确过滤,但它区分大小写。
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.我会假设(无法测试 C#),它就像将.ToLower()
添加到集合项字符串和搜索字符串一样简单。 Here it is applied to a line in your code:在这里,它应用于代码中的一行:
var myFilter = new Predicate<object>(item => ((ProductRow)item).ProductName.ToLower().Contains(searchTerm.ToLower()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.