Compare rows of a datagridview and remove repeated rows

15,209

Solution 1

for (int currentRow = 0; currentRow < grv.Rows.Count; currentRow++)
{
   var rowToCompare = grv.Rows[currentRow]; // Get row to compare against other rows

   // Iterate through all rows 
   //
   foreach (var row in grv.Rows)
   {  
       if (rowToCompare.equals(row) continue; // If row is the same row being compared, skip.

       bool duplicateRow = true;

       // Compare the value of all cells
       //
       for (int cellIndex; cellIndex < row.Cells.Count; cellIndex++)
       {
           if ((null != rowToCompare.Cells[cellIndex].Value) && 
               (!rowToCompare.Cells[cellIndex].Value.equals(row.Cells[cellIndex].Value)))
          {
             duplicateRow = false;
             break;
          }
       }

       if (duplicateRow)
       {
           grv.Rows.Remove(row);
       }
   }
}

Solution 2

This is cleaner and involves less compare.

    public void RemoveDuplicate(DataGridView grv)
    {
        for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
        {
            DataGridViewRow rowToCompare = grv.Rows[currentRow]; 

            for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
            {
                DataGridViewRow row = grv.Rows[otherRow];

                bool duplicateRow = true;

                for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                {
                    if (!rowToCompare.Cells[cellIndex].Value.Equals(row.Cells[cellIndex].Value))
                    {
                        duplicateRow = false;
                        break;
                    }
                }

                if (duplicateRow)
                {
                    grv.Rows.Remove(row);
                    otherRow--;
                }
            }
        }
    }
Share:
15,209

Related videos on Youtube

greg dorian
Author by

greg dorian

Updated on August 19, 2022

Comments

  • greg dorian
    greg dorian over 1 year

    I am trying to compare the rows of a Datagridview and that it removes the rows that are repeated. I think that I´m doing something wrong. Here´s the code:

     public void Compare(DataGridView grv)
        { 
         grv.Sort(grv.Columns[0],ListSortDirection.Ascending);
         for ( int row = 0; row < grv.Rows.Count; row++)
         {
         for ( int col = 0; col < grv.Columns.Count; col++)
         {
             int rowx=1;
             if (grv.Rows[row].Cells[col].Value != null && grv.Rows[row].Cells[col].Value.Equals(grv.Rows[rowx].Cells[col].Value))
             {
                 if (col == grv.Columns.Count - 1)
                 {
                     grv.Rows.RemoveAt(row);
                     grv.Sort(grv.Columns[0], ListSortDirection.Descending);
                 }
             }
             else
             {
                 grv.FirstDisplayedScrollingRowIndex = grv.RowCount - 1;
                 grv.Rows[grv.RowCount - 1].Selected = true;
             }
            }
         }
        }
    
  • d.moncada
    d.moncada over 11 years
    @gregdorian no problem! I believe you may want to change the foreach to a for loop because you can't remove an item from a list that is being iterated using foreach. – moncadad 7 mins ago
  • Victor Zakharov
    Victor Zakharov about 10 years
    If this rowToCompare.Cells[cellIndex].Value is null, you will get an exception.
  • Victor Zakharov
    Victor Zakharov about 10 years
    If rowToCompare.Cells[cellIndex].Value is null, you will get an exception.