C# Find duplicate Values in Datagridview

徘徊边缘 提交于 2019-12-23 05:40:33

问题


doing some user interface restriction on my c sharp project. using visual studio 2008 and C#.net.

So I have a little bit of code, its a nested for loop that should run through the columns rows and check if there's a duplicate.

Thinking about it I should change the text to an array that I can print out since there can be more than 1 duplicate.

Simply put, there is a league of parts, incrementing by one to be unique. The user wish's to change the league parts, some go up some go down.

here's what I have so far.

 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;
                    }
                }
            }
        }
    }

Nothing happens, nothing at all seems it's always false. some odd reason debugging isn't catching anything. So I'm wandering if there's a silly one liner I've missed out, or if theres a much better way to do it?

Many thanks!


回答1:


Select Distinct in LINQ should do this. Using the method syntax, something like:

    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;
    }
    }


来源:https://stackoverflow.com/questions/10279723/c-sharp-find-duplicate-values-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!