使用BindingSoure.Filter或DataView.RowFilter中的Like过滤整数

问题描述 投票:1回答:1

我试图过滤一个有多列的DataView,其中一些是数值。键入字符串时一切都很完美,但一旦输入数值(如检查信使#),它就会过滤掉。

过滤前我的数据如何显示:enter image description here

过滤后我的数据如何显示:enter image description here

我的守则如下:

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;
    }

我在网上搜索解决方案但找不到任何有用的解决方案。为什么会发生这种情况,我该如何解决?

c# .net winforms dataview bindingsource
1个回答
7
投票

请考虑以下注释:

  • 您已将列名称放在''之间,使其成为字符串文字。
  • 如果它是一个复杂的名称,请在列名称周围使用[]
  • 要比较integer列和LIKE运算符,首先应将其转换为string,因为LIKEstring运算符。
  • 还要确保您的列名称为Courier #,而不是标题/标题文本。

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

更多信息

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

© www.soinside.com 2019 - 2024. All rights reserved.