highlighting duplicate rows in datagridview

dualshck012

Active member
Joined
Jan 14, 2018
Messages
29
Location
Leyte, Philippines
Programming Experience
1-3
Good Day! I have this code to highlight a duplicated row in datagridview, but cannot make the highlighting work. My datagridview is set to AutoGenerateColumns = true and getting my column name headers from my sql query.

C#:
public void _HIGHLYTSAME(DataGridView dgtable_dmgrphic)
{
    for (int currow = 0; currow < dgtable_dmgrphic.Rows.Count - 1; currow++)
    {
        DataGridViewRow rcompare = dgtable_dmgrphic.Rows[currow];

        for (int othrow = currow + 1; othrow < dgtable_dmgrphic.Rows.Count; othrow++)
        {
            DataGridViewRow row = dgtable_dmgrphic.Rows[othrow];
            bool duprow = true;
            if (!rcompare.Cells["PROFILE"].Value.Equals(row.Cells["PROFILE"].Value))
            {
                duprow = false;
                break;
            }
            if (duprow)
            {
                rcompare.DefaultCellStyle.BackColor = Color.Green;
                rcompare.DefaultCellStyle.ForeColor = Color.#fff;
                row.DefaultCellStyle.BackColor = Color.Green.;
                row.DefaultCellStyle.ForeColor = Color.yellow;
            }
        }
    }
}
 
Last edited by a moderator:
Have you debugged your code, i.e. set a breakpoint and stepped through it line by line? I'll wager not. You should do so as it will be quite instructive. There's little doubt that that code is not doing what you think it is and debugging is the way you work out what it actually is doing. Consider this: does it really make sense to break out of the inner loop when you encounter a row that isn't a duplicate?
 

Latest posts

Back
Top Bottom