本文介绍了使用BindingSoure.Filter或DataView.RowFilter中的Like过滤整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图过滤具有多个列的 DataView ,其中有些是数字值.键入字符串时,一切都可以正常工作,但是一旦输入数值(例如检查信使#),它将过滤为空.

I've Trying to filter a DataView which have multiple columns where some is numerical values. Everything works perfectly when typing in a string but once a numerical value gets entered (like checking for courier #) it filters to nothing.

我的数据在过滤之前如何显示:

How My Data Displays before filter:

过滤后我的数据如何显示:

How My Data Displays after filter:

我的代码如下:

private void tbxSearchCourier_KeyUp(object sender, KeyEventArgs e)
    {
        string outputInfo = "";
        string[] keyWords = tbxSearchCourier.Text.Split(' ');

        foreach (string word in keyWords)
        {
            if (outputInfo.Length == 0)
            {
                outputInfo = "('Courier #' LIKE '%" + word + "%' OR Name LIKE '%" + word + "%' OR Branch LIKE '%" + word + "%' OR 'Contact Number' LIKE '%" + word
                    + "%' OR 'Email Address' LIKE '%" + word + "%')";
            }
            else
            {
                outputInfo += " AND ('Courier #' LIKE '%" + word + "%' OR Name LIKE '%" + word + "%' OR Branch LIKE '%" + word + "%' OR 'Contact Number' LIKE '%" + word
                    + "%' OR 'Email Address' LIKE '%" + word + "%')";
            }
        }
        CourierDV.RowFilter = outputInfo;
    }

我已经在网络上寻找解决方案,但找不到任何有效的方法.为什么会发生这种情况,我该如何解决?

I've search for a solution on the web but can't find anything that works. Why is this happening and how can I fix it?

推荐答案

请考虑以下注意事项:

  • 您已将列名放在''之间,这使其成为字符串文字.

  • You've put the column name between '' which makes it as a string literal.

如果列名是复杂名称,请在列名周围使用 [] .

Use [] around column name if it's a complex name.

要将整数列与 LIKE 运算符进行比较,应首先将其转换为 string ,因为 LIKE string 运算符.

To compare an integer column with LIKE operator, you should first convert it to string because LIKE is an string operator.

还要确保您的列名是 Courier#,而不是标题/标题文本.

Also make sure your column name is Courier # and it's not the caption/header text.

示例

dataView.RowFilter = "Convert([Some Column], System.String) LIKE '12%'";

更多信息

要了解有关过滤器支持的语法的更多信息,请查看 DataColumn.Expression .

To learn more about supported syntax for filter, take a look at DataColumn.Expression.

这篇关于使用BindingSoure.Filter或DataView.RowFilter中的Like过滤整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:04