【发布时间】:2012-06-27 13:10:51
【问题描述】:
早安。
使用 C sharp .net4 和 MS Visual Studio 2010。
我为我的 Windows 窗体程序开发了一个重复检查器。 当有几百条记录时,它可以完美运行,并且在我的 Datagrid 上几乎是即时的。
我注意到的问题是,当显示 6000 条记录时,它根本就不够高效,需要几分钟。
如果有人有一些好的技巧可以让这种方法更快地改进现有设计,或者我已经看过的不同方法,我正在徘徊。
再次感谢您的帮助!
代码如下:
public void CheckForDuplicate()
{
DataGridViewRowCollection coll = ParetoGrid.Rows;
DataGridViewRowCollection colls = ParetoGrid.Rows;
IList<String> listParts = new List<String>();
int count = 0;
foreach (DataGridViewRow item in coll)
{
foreach (DataGridViewRow items in colls)
{
count++;
if ((items.Cells["NewPareto"].Value != null) && (items.Cells["NewPareto"].Value != DBNull.Value))
{
if ((items.Cells["NewPareto"].Value != DBNull.Value) && (items.Cells["NewPareto"].Value != null) && (items.Cells["NewPareto"].Value.Equals(item.Cells["NewPareto"].Value)))
{
if ((items.Cells["Part"].Value != DBNull.Value) && (items.Cells["Part"].Value != null) && !(items.Cells["Part"].Value.Equals(item.Cells["Part"].Value)))
{
listParts.Add(items.Cells["Part"].Value.ToString());
dupi = true; //boolean toggle
}
}
}
}
}
MyErrorGrid.DataSource = listParts.Select(x => new { Part = x }).ToList();
}
如有任何问题,请告诉我,我会尽力回答。
【问题讨论】:
-
一个简单的优化,不会对你的代码有太大的改变,不要把所有的列都看两次,外循环循环所有列,内循环从外循环所在的地方循环到最后
-
另外。 Cells["NewPareto"] 是一个字符串查找。如果你在循环之外得到 NUMBER(表格不会在循环中改变),然后通过数字访问呢?
-
你最初的原因是我循环获取第一个值,然后使用它作为基础,我通过第二个循环检查所有其他值以查看是否有匹配项。
-
@StevenSmith 如果您查看了我的答案,我应该补充一点,我刚刚更改了它以获得更好的解决方案,这意味着您只需为每一行进行一次单元格查找。
-
啊,我看看这让我花了多少时间!
标签: c# .net winforms datagridview duplicates