BindingSource.Filter 如何实现模糊查询

mathieuxiao 2009-03-14 11:16:51
希望通过Filter属性进行过滤
BindingSource.Filter = "%p"; //ok
BindingSource.Filter = "%p%"; //ok
BindingSource.Filter = "%p%c%"; //wrong,提示表达式错误
如果希望进行第三种方式的模糊查询,应该怎么办?
...全文
1410 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2012-04-20
  • 打赏
  • 举报
回复
没有更简便的方法了吗 filter是否可以?
Dobzhansky 2009-03-14
  • 打赏
  • 举报
回复
楼主不懂 BindingSource, 过滤不是由 bindingsource 来执行的,
是 bindingsource 照看的底层数据源实现的, bindingsource 只负责传递.
你的可能是 DataTable,

你用 bindingsource 管理一个 List 数据源试试, 连基本的过滤都不支持.

如果 DataTable 的过滤不够用, 你需要写自己的数据源.
实现众多接口.
mathieuxiao 2009-03-14
  • 打赏
  • 举报
回复
通配符
cppfaq 2009-03-14
  • 打赏
  • 举报
回复
没看懂%P是啥意思。
楼主把你的问题上下文再描述清楚一点?
cppfaq 2009-03-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hulihui 的回复:]
"%p%c%"估计到数据库时将产生死循环
[/Quote]
不会死循环,在SQL里面能够正确执行。
cppfaq 2009-03-14
  • 打赏
  • 举报
回复
刚才用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;
}
mathieuxiao 2009-03-14
  • 打赏
  • 举报
回复
怎么解决呢?多关键字难道就不能用了
长沙三毛 2009-03-14
  • 打赏
  • 举报
回复
"%p%c%"估计到数据库时将产生死循环
mathieuxiao 2009-03-14
  • 打赏
  • 举报
回复
我已经试过了,通配符在前或在后都没有问题,就是不能在中间,所以如果想查"p1c","p222c","pc"这样的数据就有问题

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧