【发布时间】:2020-09-08 06:59:04
【问题描述】:
我一直在尝试设置基于 4 个文本框的过滤器。如果第一个文本框不为空 -> 然后根据它进行过滤,如果第一个和第二个文本框不为空,则从这两个文本框等中组合过滤器。我希望我的过滤器能够像这样工作http://www.tablefilter.com/auto-filter.html
如您所见,我尝试了几种变体,但我不断收到下面提供的错误。有什么建议可以让它工作吗?
这是我的代码:
public void EnableRowFiltering()
{
StringBuilder sb = new StringBuilder();
if (this.YRNROSearchKey != string.Empty)
{
sb.Append($"YRNRO LIKE '%{this.YRNROSearchKey}%' AND ");
}
if (this.HAKUNIMISearchKey != string.Empty)
{
sb.Append($"HAKUNIMI LIKE '%{this.HAKUNIMISearchKey}%' AND ");
}
if (this.GROUPSearchKey != string.Empty)
{
sb.Append($"KONSERNI LIKE '%{this.GROUPSearchKey}%' AND ");
}
if (this.BUSINESSIDSearchKey != string.Empty)
{
sb.Append($"LY LIKE '%{this.BUSINESSIDSearchKey}%' AND ");
}
// I have tried also this way without success
// this.MainDataTable.DefaultView.RowFilter = YRNRO + HAKUNIMI + GROUP + BUSINESSID;
string YRNRO = string.IsNullOrEmpty(this.YRNROSearchKey) ? "" : $"YRNRO LIKE '{this.YRNROSearchKey}*'";
string HAKUNIMI = string.IsNullOrEmpty(this.HAKUNIMISearchKey) ? "" : $" AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'";
string GROUP = string.IsNullOrEmpty(this.GROUPSearchKey) ? "" : $" AND KONSERNI LIKE '{this.GROUPSearchKey}*'";
string BUSINESSID = string.IsNullOrEmpty(this.BUSINESSIDSearchKey) ? "" : $" AND LY LIKE '{this.BUSINESSIDSearchKey}*'";
this.MainDataTable.DefaultView.RowFilter = sb.ToString();
}
System.Data.SyntaxErrorException: '语法错误: 后面缺少操作数 '和'运算符。'
这一直有效,但我必须提供所有值(填写所有文本框)才能过滤:
public void EnableRowFiltering()
{
this.MainDataTable.DefaultView.RowFilter =
$"YRNRO LIKE '{this.YRNROSearchKey}*' " +
$"OR HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
$"OR KONSERNI LIKE '{this.GROUPSearchKey}*'" +
$"OR LY LIKE '{this.BUSINESSIDSearchKey}*'";
}
【问题讨论】: