0

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!

lemunk
  • 2,616
  • 12
  • 57
  • 87
  • Perhabs this will help you: http://stackoverflow.com/questions/9600950/how-to-count-duplicates-in-datagridview-in-c-sharp – HW90 Apr 23 '12 at 11:39
  • What's strange is it does in fact work, and sets a bool to true if there is a duplicate. For some reason though it wont print anything to the text box. any reasons? – lemunk Apr 23 '12 at 14:04

1 Answers1

0

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;
    }
    }
Judo
  • 5,167
  • 3
  • 24
  • 34