【问题标题】:Compare rows of a datagridview and remove repeated rows比较数据网格视图的行并删除重复的行
【发布时间】:2012-11-24 21:26:01
【问题描述】:

我正在尝试比较 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;
         }
        }
     }
    }

【问题讨论】:

  • 这里没有问题。你在寻求帮助,但你没有告诉我们你遇到了什么问题。请提出一个清晰、简洁的问题。
  • ALex 的目的,是在 DatagridView 中加载一个 txt 文件并消除重复的行。
  • jhon 问题是有时不会从 datagridview 中删除行

标签: c# winforms datagridview


【解决方案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);
       }
   }
}

【讨论】:

  • @gregdorian 没问题!我相信您可能希望将 foreach 更改为 for 循环,因为您无法从使用 foreach 迭代的列表中删除项目。 – moncadad 7 分钟前
  • 如果rowToCompare.Cells[cellIndex].Value 为空,你会得到一个异常。
【解决方案2】:
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
        {
        }

【讨论】:

    【解决方案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--;
                    }
                }
            }
        }
    

    【讨论】:

    • 如果这个rowToCompare.Cells[cellIndex].Value为空,你会得到一个异常。
    • 完美!最佳答案在这里。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2023-03-24
    • 2019-12-10
    • 2023-03-15
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多