我使用下面的代码允许用户DataTable通过搜索可能位于任何列或任何行中的特定字符串来过滤 a。代码需要删除值不存在的行,因为DataTable在操作后导出。此代码的问题有两个:1) 对于较大的表,它非常慢,以及 2) 它只能找到一个单元格的完整内容(即,如果列“Name”有一行值为“Andrew “用户应该能够搜索“drew”或“and”并获得该结果;现在,如果他们搜索“Andrew”,它将返回该行)。if(!String.IsNullOrEmpty(combo1Text) || !String.IsNullOrEmpty(combo2Text) && !String.IsNullOrEmpty(search1Text) && !search1Text.Contains("Type your search for" + comboText + "here")) { for (int i = tab1table.Rows.Count - 1; i >= 0; i--) { DataRow dr = tab1table.Rows[i]; if (!dr.ItemArray.Contains(search1Text)) { dr.Delete(); tab1table.AcceptChanges(); } percentprogress++; worker.ReportProgress(percentprogress); } }进行我想要的过滤的最佳方法是什么(并且如此有效地进行过滤,以便它不仅仅是遍历所有内容)?
1 回答

慕村9548890
TA贡献1884条经验 获得超4个赞
要搜索单元格内容是否包含搜索到的文本,请尝试以下代码:
for (int i = tab1table.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = tab1table.Rows[i];
if (!dr.ItemArray.Any(x=>(x as string).Contains(search1Text)))
{
dr.Delete();
}
percentprogress++;
worker.ReportProgress(percentprogress);
}
tab1table.AcceptChanges();
如果您有任何不是字符串类型的列,则应替换(x as string)为x.ToString()
- 1 回答
- 0 关注
- 167 浏览
添加回答
举报
0/150
提交
取消