Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm populating a DataTable using a SQL query which is ordered by Environment AND Application Name, for the returned data i'd like to highlight any duplicate rows where the Application Name is the same. Here's how I'm populating my datagridview:
C#
try
{
    //Call DataProcessor class to get the records from the database
    DataProcessor dp = new DataProcessor();
    DataTable dt = dp.getRecs(txtEnvID.Text);

    //if records are returned
    if (dt.Rows.Count > 0)
    {
        //remove the below cols from the datatable before binding to the
        //datagridview object - do not wish to display these details
        dt.Columns.Remove("ENV_ROWVERSION_TS");
        dt.Columns.Remove("ENVA_ROWVERSION_TS");
        dt.Columns.Remove("APP_ROWVERSION_TS");
        dt.Columns.Remove("ENVA_ENVIRONMENT_ID1");
        dt.Columns.Remove("ENVA_APP_ID1");
        dt.Columns.Remove("CountOf");

        //bind datatable to display grid
        dgvDuplicates.DataSource = dt;
    }


Can anyone enlighten me as to how I check for duplicate rows?

thank you!
Posted

Solved this with this code - hope this helps someone else in future:
C#
public void HighlightDuplicate(DataGridView grv)
{
    //use the currentRow to compare against
    for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
    {
        DataGridViewRow rowToCompare = grv.Rows[currentRow];
        //specify otherRow as currentRow + 1
        for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
        {
            DataGridViewRow row = grv.Rows[otherRow];

            bool duplicateRow = true;
            //compare cell ENVA_APP_ID between the two rows
            if (!rowToCompare.Cells["ENVA_APP_ID"].Value.Equals(row.Cells["ENVA_APP_ID"].Value))
            {
                duplicateRow = false;
                break;
            }
            //highlight both the currentRow and otherRow if ENVA_APP_ID matches 
            if (duplicateRow)
            {
                rowToCompare.DefaultCellStyle.BackColor = Color.Red;
                rowToCompare.DefaultCellStyle.ForeColor = Color.Black; 
                row.DefaultCellStyle.BackColor = Color.Red;
                row.DefaultCellStyle.ForeColor = Color.Black; 
            }
        }
    }
}
 
Share this answer
 
Comments
Sushil Mate 23-Jul-13 6:23am    
please marked as a solution then.
Jared Nathan Drake 24-Aug-15 11:50am    
Hi, please refrain using 'break;' otherwise, there will be no highlight. 'break;' will stop the 'for loop'. I just removed 'break;' and it works perfect! Thank you very much pmcm! 9 out of 10 stars just for you :)
Jared Nathan Drake 24-Aug-15 11:56am    
And I'm trying to figure something out on how to change the different backcolor of the different value, for example: For a value of duplicate "John", it should be Red, and another value of 'Jared', it should be Blue, and the rest of them should be different color.
CellFormatting would do the trick in this case.So check application name column for each row and change color like..
C#
private void MyDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        if (e.RowIndex == rowIndexToHighlight)
        {
            e.CellStyle.BackColor = Color.Green;
        }

    }

These also help you..
http://forums.asp.net/p/1654794/4657253.aspx/1?Re+Highlight+duplicate+values+in+gridview[^]
http://www.nullskull.com/q/10063889/gridview-formatting-for-desktop-app.aspx[^]
 
Share this answer
 
v2
Comments
pmcm 22-Jul-13 8:27am    
not sure this is exactly what I am after. I'd like to compare a col in row 1 with the same col in row 2 and if the values match then highlight the rows
ridoy 22-Jul-13 8:29am    
i only show the code to highlight color,not all the code you need.So you need to compare the values of row and then change the color with DataGridViewCellFormattingEventArgs event like above one.
pmcm 22-Jul-13 10:24am    
i'm using this code to try to compare the values:
DataGridViewRowCollection coll = dgvDuplicates.Rows;
DataGridViewRowCollection colls = dgvDuplicates.Rows;
//List<string> listParts = new List<string>();
int count = 0;
foreach (DataGridViewRow item in coll)//379
{
foreach (DataGridViewRow items in colls)//143641
{
if ((items.Cells["ENVA_ENVIRONMENT_APP_ID"].Value != null))
{
if ((items.Cells["ENVA_APP_ID"].Value != null) && (items.Cells["ENVA_APP_ID"].Value.Equals(item.Cells["ENVA_APP_ID"].Value)))
{
item.DefaultCellStyle.BackColor = Color.Red;
item.DefaultCellStyle.ForeColor = Color.Black;
}
}
}
count++;
}

but instead of iterating through the result records to compare against, this is always comparing the 1st row against each of the subsequent rows which is not what I want. I'd like to compare Row2 against Row1, Row3 against Row2, Row4 against Row3 and so on. Can you see what I'm doing wrong?
pmcm 22-Jul-13 8:51am    
can you point me in the right direction to another link or site (anything I have looked at so far has not help me yet)to compare the values of row. Do I need to do this comparison after binding the data to the DataGrid or would the RowsAdded Event be better?
ridoy 22-Jul-13 10:38am    
There are no accurate things which you want but i think basically it would be
int value1 = Convert.ToInt32(dataGridView1.Rows[0].Cells["Column1"].Value.ToString());
int value2 = Convert.ToInt32(dataGridView1.Rows[1].Cells["Column1"].Value.ToString());
now find those rows where value1==value2.I show here for 2 rows of value.when you have more then loop through the rows and pick those values in an array value[] and then compare values.Have some idea from http://www.vbforums.com/showthread.php?594225-Comparing-DataGridView-Rows, though it is in vb.net,but logic would be same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900