This way
HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Namespaces
using System.Data;
using System.Drawing;
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(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)
{
//use the currentRow to compare against
for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
{
GridViewRow rowToCompare = grv.Rows[currentRow];
//specify otherRow as currentRow + 1
for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
{
GridViewRow row = grv.Rows[otherRow];
bool duplicateRow = true;
//compare cell ENVA_APP_ID between the two rows
if (rowToCompare.Cells[0].Text != row.Cells[0].Text)
{
duplicateRow = false;
break;
}
//highlight both the currentRow and otherRow if ENVA_APP_ID matches
if (duplicateRow)
{
rowToCompare.BackColor = Color.Red;
rowToCompare.ForeColor = Color.Black;
row.BackColor = Color.Red;
row.ForeColor = Color.Black;
}
}
}
}
Screenshot
