none
Search DataGridView for duplicates RRS feed

  • Question

  • I have this code to find duplicate values in DataGridView and mark them with different colors.

    var rows = dataGridView1.Rows.OfType<DataGridViewRow>().Reverse().Skip(1); //ignore the last empty line
    var dupRos = rows.GroupBy(r => r.Cells["Date"].Value.ToString()).Where(g => g.Count() > 1).SelectMany(r => r.ToList());
    
    foreach (var r in dupRos)
    {
        r.DefaultCellStyle.BackColor = Color.Pink;
    }
    foreach (var r in rows.Except(dupRos))
    {
        r.DefaultCellStyle.BackColor = Color.Cyan;
    }

    The code works fine.

    I have changed the code so it will write in the second column the word Unique or Duplicate and a counter number for the duplicate cells.

    int counter = 1;
    var rows = dataGridView1.Rows.OfType<DataGridViewRow>().Reverse().Skip(1); //ignore the last empty line
    var dupRos = rows.GroupBy(r => r.Cells["Date"].Value.ToString()).Where(g => g.Count() > 1).SelectMany(r => r.ToList());
    
    foreach (var r in dupRos)
    {
        r.DefaultCellStyle.BackColor = Color.Pink;
        r.Cells["Time"].Value = "Dup" + counter;
        counter++;
    }
    foreach (var r in rows.Except(dupRos))
    {
        r.DefaultCellStyle.BackColor = Color.Cyan;
        r.Cells["Time"].Value = "Unick";
    }

    My problem is that the counter continues to count for all the duplicate groups and not reset itself every time its start with a different group of duplicate values.

    How can I fix it?


    Monday, October 14, 2019 11:03 AM

All replies

  • Another idea is to simply group only duplicates and shown as follows. If interested I will clean up the code which I just wrote and upload to Microsoft OneDrive.

    Note counter column is the indexer for each group of duplicates.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 14, 2019 12:12 PM
    Moderator
  • Hi Barak Aricha Tamir,

    Thank you for posting here.

    Based on your description, you want to mark the duplicate cells in DataGridView and record the number of repetitions.

    In your code, you put all the duplicate cells in one collection, but in fact, you can put different duplicate cells into different collections.

    I make a code example and you can refer to it.


                int counter = 1;
                var rows = dataGridView1.Rows.OfType<DataGridViewRow>().Reverse().Skip(1); //ignore the last empty line
                var dupRos = rows.GroupBy(r => r.Cells["Date"].Value.ToString()).Where(g => g.Count() > 1).SelectMany(r => r.ToList());
    
                IEnumerable<IGrouping<Object,DataGridViewRow>> groups =
                         from row in rows
                         group row by row.Cells["Date"].Value into newGroup
                         select newGroup;
    
                foreach (var group in groups)
                {
                    foreach (var row in group)
                    {
                        row.DefaultCellStyle.BackColor = Color.Pink;
                        row.Cells["Time"].Value = "Dup" + counter;
                        counter++;
                    }
                    counter = 1;
                }
                foreach (var r in rows.Except(dupRos))
                {
                    r.DefaultCellStyle.BackColor = Color.Cyan;
                    r.Cells["Time"].Value = "Unick";
                }

    Result:

    Hope my solution could be helpful.

    Best regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Tuesday, October 15, 2019 3:28 AM