【问题标题】:C# Find duplicate Values in DatagridviewC# 在 Datagridview 中查找重复值
【发布时间】:2012-05-04 00:23:15
【问题描述】:

对我的 c sharp 项目做一些用户界面限制。 使用 Visual Studio 2008 和 C#.net。

所以我有一点代码,它是一个嵌套的 for 循环,应该遍历列行并检查是否有重复。

考虑一下,我应该将文本更改为可以打印出来的数组,因为可以有超过 1 个重复项。

简单地说,有一个零件联盟,加一是唯一的。用户希望改变联赛部分,有的升有降。

这是我目前所拥有的。

 public void CheckForDuplicate()
    {
        DataGridViewRowCollection coll = ParetoGrid.Rows;
        DataGridViewRowCollection colls = ParetoGrid.Rows;

        foreach (DataGridViewRow item in coll)
        {
            foreach (DataGridViewRow items in colls)
            {
                if (items.Cells[5].Value == item.Cells[5].Value)  
                {   
                    if(items.Cells[2].Value != item.Cells[2].Value)
                    {
                        txtDupe.Text = items.Cells[2].Value.ToString();
                        this.Refresh();
                        dupi = false;
                    }
                }
            }
        }
    }

什么都没有发生,似乎什么都没有,它总是假的。一些奇怪的原因调试没有捕捉到任何东西。 所以如果我错过了一个愚蠢的班轮,或者如果有更好的方法来做到这一点,我会徘徊?

非常感谢!

【问题讨论】:

  • 也许这会对你有所帮助:stackoverflow.com/questions/9600950/…
  • 奇怪的是它确实有效,如果有重复项,它会将 bool 设置为 true。出于某种原因,尽管它不会在文本框中打印任何内容。有什么原因吗?

标签: c# datagridview duplicates


【解决方案1】:

在 LINQ 中选择 Distinct 应该这样做。使用方法语法,类似于:

    public bool allUniqueRows()
    {
        var distinctCount =  from r in ParetoGrid.Rows
select r.whateverElement.Distinct().Count();
     if(distinctCount == ParetoGrid.Rows.Count())
    {
    return true;
    }
    else
    {
    return false;
    }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2010-10-15
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多