【发布时间】:2023-03-23 18:55:01
【问题描述】:
我有一个DataGridView,它从几个表中提取数据,类似于this 设置。它工作得很好。好帖子和答案。继续该帖子中的示例,我现在想创建一个过滤器,通过在帐户描述中使用 LIKE 参数,生成 DataGridView 中适用于特定帐户的所有交易。
我有一个解决方案,方法是检查帐户表中的描述并获取 IDAccount 值,然后在 DataGridView 过滤器中使用该值,但我希望有一种更自动化的方式使用绑定。
有什么想法吗?谢谢你的建议。
编辑:
假设我有一个名为 AccountDescriptionBox 的TextBox 控件,我希望能够执行类似的操作
dataGridView1.Filter = string.Format("{0} LIKE '{1}'", "IDAccount", AccountDescriptionBox.Text);
显然,这不起作用,因为 IDAccount 是整数,而不是字符串。我上面提到的解决方案是
string filter = string.Empty;
Regex searchTerm = new Regex(Regex.Escape(AccountDescriptionBox.Text).Replace('\\', '.'), RegexOptions.IgnoreCase);
var accts = from acct in dataSet1.Accounts
let matches = searchTerm.Matches(acct.Description)
where matches.Count > 0
select acct.ID;
for (int i; i < accts.Count() - 1; i++)
{
filter += string.Format("IDAccount = {0} OR ",accts.ElementAt(i));
}
filter += string.Format("IDAccount = {0}",accts.Last());
dataGridView1.Filter = filter;
这可行,但很麻烦。如果有办法,我宁愿通过绑定来实现。
【问题讨论】:
-
代码 sn-ps 来说明您要完成的工作可能会有所帮助
-
我最近也在尝试如何过滤datagridviews。在我的情况下,它们由 linq 填充。我现在使用的解决方案是简单地使用 linq 查询作为过滤器,因此过滤发生在行最终进入 datagridview 之前。根据您的情况,可能对您有用。
-
@CodeBlend,查看我的编辑。
-
您能否在不使用真实世界语句的代码的情况下提供文本示例 AKA 来说明您的要求,例如结果表应显示:所有医生按看过患者“John Smith”的医生过滤
-
如果我有名为“Checking”、“Savings”和“401(k)”的账户,我希望视图只显示来自名为“Savings”的账户的交易。
标签: c# datagridview filter