-1

这是我的数据网格中显示的示例数据


它是如何在 c# 中完成的?只是为这个寻找任何建议..?

4

1 回答 1

0

考虑到您的图片代表您创建的网格,
请调用此方法来查找重复项:

/// <summary>
/// Finds the duplicates inside of a DataGridView
/// </summary>
/// <param name="grid">The instance of the DataGridView</param>
/// <param name="columnNamesToCompare">Collection of the Column.Name to compare</param>
/// <returns>List of KeyValuePairs of the index and list of it's duplicates</returns>
public static IEnumerable<KeyValuePair<int, List<int>>> FindDuplicates(this DataGridView grid, ICollection<string> columnNamesToCompare) {
        var dupesLog = new List<int>(); //represents rows that were marked as duplicates
        var locDupes = new List<int>(); //collector for the yield return
        for (int i = 0; i < grid.Rows.Count; i++) {
            if (dupesLog.Contains(i)) continue;
            locDupes.Clear();
            for (int j = 0; j < grid.Rows.Count; j++) {
                if (j==i) continue;
                foreach (string column in columnNamesToCompare) {
                    if (grid.Rows[i].Cells[column].Value.Equals(grid.Rows[j].Cells[column].Value) == false)
                        goto _next;
                }
                //if it got to here, means it is true
                locDupes.Add(j);
                dupesLog.Add(j);
                _next:
                continue;
            }
            if (locDupes.Count > 0)
                yield return new KeyValuePair<int, List<int>>(i, locDupes);
        }
    }

可能有更有效的方法来做到这一点,但这是我会做的。
我没有测试它,但试一试。

于 2013-07-25T02:54:35.713 回答