我想查找gridview中不同行的两个单元格是否具有相同的值。 我使用这段代码,但数百万的记录,需要很长时间。你能告诉我一个最佳方式吗?
任何人都可以建议我一个最佳方式
答案 0 :(得分:0)
在'otherRow'for statement中,grv.Rows.Count中缺少-1。
public void HighlightDuplicate(GridView grv)
{
//use the currentRow to compare against
for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
{
GridViewRow rowToCompare = grv.Rows[currentRow];
//specify otherRow as currentRow + 1
//This forloop loops over more rows then exist
for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count -1; otherRow++)
{
GridViewRow row = grv.Rows[otherRow];
bool duplicateRow = true;
//compare cell ENVA_APP_ID between the two rows
if (!rowToCompare.Cells["ENVA_APP_ID"].Text.Equals(row.Cells["ENVA_APP_ID"].Text))
{
duplicateRow = false;
break;
}
//highlight both the currentRow and otherRow if ENVA_APP_ID matches
if (duplicateRow)
{
rowToCompare.BackColor = System.Drawing.Color.Red;
}
}
}
}