Datagridview 的 VB.NET BindingSource 过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16861706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:49:20  来源:igfitidea点击:

VB.NET BindingSource filter for Datagridview

vb.netdatagridviewfilterbindingsource

提问by CRAIGRY

In MSSQL I can filter a query on a phone number like this:

在 MSSQL 中,我可以过滤电话号码的查询,如下所示:

where replace(phone,'-','') Like '%480555%'

I am trying to figure out how to do this on a Datasource. A normal query looks like this:

我想弄清楚如何在数据源上执行此操作。正常查询如下所示:

Dim stringFilter As String = String.Empty
String.Format("phone Like '%480555%'")
ViewCustomersBindingSource.Filter = stringFilter

However, this will not find any results because the datasource has the values with hyphens in it. REPLACE is not a valid argument for filtering.

但是,这不会找到任何结果,因为数据源中包含带连字符的值。REPLACE 不是过滤的有效参数。

My initial thought was to update the MSSQL View to strip the hyphens. However, for display, I would want to display the hyphens. I can not assume they will all look the same as some phone numbers might be a different country than the US.

我最初的想法是更新 MSSQL 视图以去除连字符。但是,为了显示,我想显示连字符。我不能假设它们看起来都一样,因为有些电话号码可能是与美国不同的国家/地区。

Is there another way to filter on a telephone number and ignore the hyphens?

还有另一种方法可以过滤电话号码并忽略连字符吗?

采纳答案by Chris

I dont think you can directly set a filter that ignores the hyphens (as in your first query) on your binding source. There is no String operations possible in the filter expression column. From this http://msdn.microsoft.com/fr-fr/library/system.windows.forms.bindingsource.filter.aspxthe only string operation allowed is +.

我不认为你可以直接设置一个过滤器来忽略你的binding source. 过滤器表达式列中不可能进行字符串操作。从这个http://msdn.microsoft.com/fr-fr/library/system.windows.forms.bindingsource.filter.aspx允许的唯一字符串操作是+.

But you can keep and display the original column which contains the hyphens, and add another column (without the hyphens) that you do not display, but that you use just for filtering.

但是您可以保留并显示包含连字符的原始列,并添加另一列(不带连字符)但您不显示但仅用于过滤的列。

With MSSQL, you can for example add a computed columnthat uses the REPLACEfunction.

例如,您可以使用MSSQL添加一个使用REPLACE函数的计算列

Here is somes links that could help you to create a computed column:

以下是一些可以帮助您创建计算列的链接:

Hope this helps.

希望这可以帮助。