Different row color for different duplicate row in GridView using C# in ASP.Net

Last Reply on Aug 21, 2017 08:05 AM By dharmendr

Posted on Aug 21, 2017 05:18 AM

hi

How to Highlight Duplicate records in Gridview with Different background color

if grid has more than 3 Diffrent Duplicate Records then add diffrent back color for this as 1 duplicated as Red, Second Dupllicated as Yellow and So on.

 

thanks in advance

Posted on Aug 21, 2017 08:05 AM

Hi crajesh,

Your requirement is not a valid one. But still you can achieve this using the below code. You need to add switch condition as i have added if your duplicate row increases. Refer the below sample.

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
        HighlightDuplicate(this.GridView1);
    }
}

public void HighlightDuplicate(GridView grv)
{
    int duplicateCount = 1;
    for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
    {
        GridViewRow rowToCompare = grv.Rows[currentRow];
        for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
        {
            GridViewRow row = grv.Rows[otherRow];
            bool duplicateRow = true;
            if ((rowToCompare.Cells[0].Text != row.Cells[0].Text)
                && (rowToCompare.Cells[1].Text != row.Cells[1].Text)
                && (rowToCompare.Cells[2].Text != row.Cells[2].Text))
            {
                duplicateRow = false;
                break;
            }
            if (duplicateRow)
            {
                switch (duplicateCount)
                {
                    case 1:
                        rowToCompare.BackColor = Color.Red;
                        rowToCompare.ForeColor = Color.Black;
                        row.BackColor = Color.Red;
                        row.ForeColor = Color.Black;
                        break;
                    case 2:
                        rowToCompare.BackColor = Color.Green;
                        rowToCompare.ForeColor = Color.Black;
                        row.BackColor = Color.Green;
                        row.ForeColor = Color.Black;
                        break;
                    case 3:
                        rowToCompare.BackColor = Color.Yellow;
                        rowToCompare.ForeColor = Color.Black;
                        row.BackColor = Color.Yellow;
                        row.ForeColor = Color.Black;
                        break;
                    default:
                        break;
                }
                duplicateCount++;
            }
        }
    }
}

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Output

IdNameCountry
1 John Hammond United States
2 Mudassar Khan India
2 Mudassar Khan India
3 Suzanne Mathews France
3 Suzanne Mathews France
4 Robert Schidner Russia
4 Robert Schidner Russia