Change GridView row color for duplicate records using C# in ASP.Net

Last Reply 27 days ago By pandeyism

Posted 27 days ago

I have a gridview in which i want to change color of that rows which are same data or duplicated data.

        protected void gvOwnerOpportunity_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowState == DataControlRowState.Alternate)
            {
                for (int i = gvOwnerOpportunity.Rows.Count - 1; i > 0; i--)
                {
                    GridViewRow row = gvOwnerOpportunity.Rows[i];
                    GridViewRow previousRow = gvOwnerOpportunity.Rows[i - 1];
                    HiddenField hdnOpp = row.Cells[0].FindControl("hdnOppId") as HiddenField;
                    HiddenField hdnOppPrev = previousRow.Cells[0].FindControl("hdnOppId") as HiddenField;
                    if (hdnOpp.Value == hdnOppPrev.Value)
                    {
                        e.Row.BackColor = System.Drawing.Color.DimGray;
                    }
                    else
                    {
                        e.Row.BackColor = System.Drawing.Color.White;
                    }
                }
            }
        }