比较datagridview的行并删除重复的行

时间:2012-12-06 18:58:46

标签: c# winforms datagridview

我试图比较Datagridview的行,并删除重复的行。 我认为我做错了什么。这是代码:

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

3 个答案:

答案 0 :(得分:3)

这是更清洁,涉及的比较少。

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

答案 1 :(得分:2)

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

答案 2 :(得分:-1)

try

        {
            if (dgv_Language.Rows.Count > 2)
            {

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

                    // Iterate through all rows 
                    //
                    foreach (DataGridViewRow row in dgv_Language.Rows)
                    {
                        if (rowToCompare.Equals(row))
                        {
                            continue;
                        }
                        // If row is the same row being compared, skip.

                        bool duplicateRow = true;

                        // Compare the value of all cells
                        //

                        if (rowToCompare.Cells[0].Value != null && rowToCompare.Cells[0].Value.ToString() != row.Cells[0].Value.ToString())
                        {
                            duplicateRow = false;

                        }


                        if (duplicateRow)
                        {
                            dgv_Language.Rows.RemoveAt(row.Index);
                            break;
                        }
                    }
                }
            }
        }
        catch
        {
        }