希望通过Filter属性进行过滤
BindingSource.Filter = "%p";  //ok
BindingSource.Filter = "%p%";  //ok
BindingSource.Filter = "%p%c%";  //wrong,提示表达式错误
如果希望进行第三种方式的模糊查询,应该怎么办?

解决方案 »

  1.   

    楼主不懂 BindingSource, 过滤不是由 bindingsource 来执行的,
    是 bindingsource 照看的底层数据源实现的, bindingsource 只负责传递.
    你的可能是 DataTable,你用 bindingsource 管理一个 List 数据源试试, 连基本的过滤都不支持.如果 DataTable 的过滤不够用, 你需要写自己的数据源.
    实现众多接口.
      

  2.   

    我已经试过了,通配符在前或在后都没有问题,就是不能在中间,所以如果想查"p1c","p222c","pc"这样的数据就有问题
      

  3.   

    刚才用Reflector看了下微软的实现,可以看出来的确不允许中间有通配符。
    internal string AnalizePattern(string pat)
            {
                int length = pat.Length;
                char[] destination = new char[length + 1];
                pat.CopyTo(0, destination, 0, length);
                destination[length] = '\0';
                string str = null;
                char[] chArray2 = new char[length + 1];
                int num3 = 0;
                int num4 = 0;
                int index = 0;
                while (index < length)
                {
                    if ((destination[index] == '*') || (destination[index] == '%'))
                    {
                        while (((destination[index] == '*') || (destination[index] == '%')) && (index < length))
                        {
                            index++;
                        }
                        if (((index < length) && (num3 > 0)) || (num4 >= 2))
                        {
                            throw new Exception("ExprException.InvalidPattern(pat);");
                        }
                        num4++;
                    }
                    else if (destination[index] == '[')
                    {
                        index++;
                        if (index >= length)
                        {
                            throw new Exception("ExprException.InvalidPattern(pat);");
                        }
                        chArray2[num3++] = destination[index++];
                        if (index >= length)
                        {
                            throw new Exception("ExprException.InvalidPattern(pat);");
                        }
                        if (destination[index] != ']')
                        {
                            throw new Exception("ExprException.InvalidPattern(pat);");
                        }
                        index++;
                    }
                    else
                    {
                        chArray2[num3++] = destination[index];
                        index++;
                    }
                }
                str = new string(chArray2, 0, num3);
                if (num4 == 0)
                {
                    kind = 4;
                    return str;
                }
                if (num3 > 0)
                {
                    if ((destination[0] == '*') || (destination[0] == '%'))
                    {
                        if ((destination[length - 1] == '*') || (destination[length - 1] == '%'))
                        {
                            kind = 3;
                            return str;
                        }
                        kind = 2;
                        return str;
                    }
                    kind = 1;
                    return str;
                }
                kind = 5;
                return str;
            }
      

  4.   

    不会死循环,在SQL里面能够正确执行。
      

  5.   

    没有更简便的方法了吗  filter是否可以?