gpt4 book ai didi

c# - 根据多个TextBoxes MVVM中的值过滤WPF DataGrid(数据表)

转载 作者:行者123 更新时间:2023-12-03 10:56:04 25 4
gpt4 key购买 nike

我一直在尝试设置基于4个TextBoxes的过滤器。如果第一个文本框不为空->然后基于此进行过滤,如果第一个和第二个文本框不为空,则将这两个文本框的过滤条件合并,依此类推。我希望我的过滤条件像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: 'Syntax error: Missing operand after'And' operator.'


这一直在工作,但是我必须提供所有值(填写所有TextBoxes)以便进行过滤:
  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}*'";
}

最佳答案

到目前为止,最后一种解决方案是最短的解决方案,所以我更喜欢它。
您只需要用AND替换OR(我不好)。

public void EnableRowFiltering()
{
this.MainDataTable.DefaultView.RowFilter =
$"YRNRO LIKE '{this.YRNROSearchKey}*'" +
$"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
$"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
$"AND LY LIKE '{this.BUSINESSIDSearchKey}*'";
}

第一个解决方案的表达式字符串具有结尾的 AND运算符,该运算符将导致错误消息。给每个表达式(操作数)加上前缀会对其进行修复。
请注意,必须删除表达式的开头 %,以使其成为“开头为”表达式。开头的 %和结尾的 %(通配符)将表示“包含于”。
因为and空字符串变量将导致类似于 "Column LIKE '%'的表达式,并且因为 ${null}返回 ""(空字符串),所以您可以缩短代码:
public void EnableRowFiltering()
{
StringBuilder sb = new StringBuilder();
sb.Append($"YRNRO LIKE '{this.YRNROSearchKey}%'");
sb.Append($"AND KONSERNI LIKE '{this.GROUPSearchKey}%'");
sb.Append($"AND LY LIKE '{this.BUSINESSIDSearchKey}%'");

this.MainDataTable.DefaultView.RowFilter = sb.ToString();
}

第二种解决方案的表达似乎是正确的,但是以某种方式混淆了分配。定义过滤器表达式以对其进行修复后,分配它。
因为and空字符串变量将导致类似于 "Column LIKE '*'的表达式,并且因为 ${null}返回 ""(空字符串),所以您可以缩短代码:
public void EnableRowFiltering()
{
string YRNRO = $"YRNRO LIKE '{this.YRNROSearchKey}*'";
string HAKUNIMI = $" AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'";
string GROUP = $" AND KONSERNI LIKE '{this.GROUPSearchKey}*'";
string BUSINESSID = $" AND LY LIKE '{this.BUSINESSIDSearchKey}*'";

this.MainDataTable.DefaultView.RowFilter = YRNRO + HAKUNIMI + GROUP + BUSINESSID;
}
在修复并改进了所有三个解决方案之后,它们全部缩减为基本相同的解决方案。

关于c# - 根据多个TextBoxes MVVM中的值过滤WPF DataGrid(数据表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63788632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com