title: C# Search datagridview for duplicates
body: <p>Using <strong>ms visual studio</strong> and <strong>csharp .net4</strong>.</p>
  
  <p>This is the code i have to check for duplicates</p>
  
  <pre><code>    public void CheckForDuplicate()
      {
          DataGridViewRowCollection coll = ParetoGrid.Rows;
          DataGridViewRowCollection colls = ParetoGrid.Rows;
          List&lt;string&gt; listParts = new List&lt;string&gt;();
          int count = 0;
          foreach (DataGridViewRow item in coll)//379
          {
              foreach (DataGridViewRow items in colls)//143641
              {
                  if (items.Cells[5].Value == item.Cells[5].Value)  
                  {
                      if (items.Cells[2].Value != item.Cells[2].Value)
                      {
                          listParts.Add(items.Cells["Keycode"].Value.ToString());
                          count++;
                          dupi = true;
  
                          //txtDupe.Text = items.Cells["Keycode"].Value.ToString();
                          //this.Refresh();
                      }
                  }
              }
          }
          MyErrorGrid.DataSource = listParts;
      }
  </code></pre>
  
  <p>This is the <strong>check</strong> before it <strong>allows the user to save</strong>.</p>
  
  <pre><code>private void butSave_Click(object sender, EventArgs e)
      {
          CheckForDuplicate();
          if (dupi == true)
          {
              txtDupe.Clear();
  
              dupi = false;
          }
          else
          {
              SaveMyWorkI();
              dupi = false;
          }
      }
  </code></pre>
  
  <p>This is the data that it is looking at:
  <img src="https://i.stack.imgur.com/D8ghk.png" alt="enter image description here"></p>
  
  <p>Now, I know the logic must be flawed since it saving regardless.
  I'm basically searching through each cell on <strong>pareto1</strong> to see if the user has made any <strong>duplicates</strong>, if so it will not save and instead displays the part number etc in another datagridview....well that's the plan.</p>
  
  <p>So could someone look through this and tell me</p>
  
  <p><strong>1)</strong> Where in my <strong>logic</strong> is this <strong>failing</strong>, also what about if the checks are correct?</p>
  
  <p><strong>2)</strong> Will the list work adding the info, if so is a simple <strong>bind to a datagrid</strong> view enough to display the results?</p>
  
  <p><strong>3)</strong> If this is just a really bad way of searching through could someone provide code that reflects what I am Trying to achieve.</p>
  
  <p>Many thanks for your future comments.</p>
  
  <p>UPDATE:: Ok thanks for the help, my algorithm now works, but my very last problem is displaying the part number that is duplicated on the pareto column, instead it displays the length.</p>
  
  <pre><code>public void CheckForDuplicate()
      {
          DataGridViewRowCollection coll = ParetoGrid.Rows;
          DataGridViewRowCollection colls = ParetoGrid.Rows;
          List&lt;string&gt; listParts = new List&lt;string&gt;();
          int count = 0;
          foreach (DataGridViewRow item in coll)//379
          { 
              foreach (DataGridViewRow items in colls)//143641
              {
                  count++;
                  if ((items.Cells[5].Value != null))
                  {
                      if ((items.Cells[5].Value != null) &amp;&amp; (items.Cells[5].Value.Equals(item.Cells[5].Value)))
                      {
                          if ((items.Cells[2].Value != null) &amp;&amp; !(items.Cells[2].Value.Equals(item.Cells[2].Value)))
                          {
                              listParts.Add(items.Cells["Keycode"].Value.ToString());
  
                              dupi = true;
                          }
                      }
                  }
              }
          }
          MyErrorGrid.DataSource = listParts;
          var message = string.Join(Environment.NewLine, listParts);
          //MyErrorGrid.DataSource = message;
          MessageBox.Show(message);
  
      }
  </code></pre>
  
  <p>Even though the message box correctly displays the results? is it something im missing out when binding to my datagrid?</p>