<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Highlight duplicate rows in gridview </title>
</head>
<body>
<form id="form1"runat="server">
<div style="width: 35%">
<fieldset style="border: medium double #FF9900; font-family: 'Times New Roman', Times, serif;
font-size: large; font-weight: bold">
<legend>
<h6>Duplicate Rows are Displayed in Yellow Color to Highlight the User</h6>
</legend>
<h3 style="font-family: 'Times New Roman', Times, serif; font-size: large; font-weight: bold;
color: #FF0000">Developed By aspsolutionkirit.blogspot.in</h3>
<asp:GridView ID="GridView1"runat="server">
</asp:GridView>
</fieldset>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bindgrid();
}
}
public void bindgrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("city");
dt.Columns.Add("mono");
DataRow dr1 = dt.NewRow();
dr1[0] = "1";
dr1[1] = "Umesh";
dr1[2] = "Ahmedabad";
dr1[3] = "1234123423";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2[0] = "2";
dr2[1] = "Chirag";
dr2[2] = "Ahmedabad";
dr2[3] = "2352348542";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3[0] = "3";
dr3[1] = "Umesh";
dr3[2] = "Amreli";
dr3[3] = "2948750222";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4[0] = "4";
dr4[1] = "Jatin";
dr4[2] = "Rajkot";
dr4[3] = "9554874412";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5[0] = "5";
dr5[1] = "Umesh";
dr5[2] = "Rajkot";
dr5[3] = "9328492234";
dt.Rows.Add(dr5);
DataRow dr6 = dt.NewRow();
dr6[0] = "4";
dr6[1] = "rahul";
dr6[2] = "Rajkot";
dr6[3] = "9554874412";
dt.Rows.Add(dr6);
GridView1.DataSource = dt;
GridView1.DataBind();
HighlightDuplicate(GridView1);
}
public void HighlightDuplicate(GridViewgridview)
{
for (int currentRow = 0; currentRow<gridview.Rows.Count - 1; currentRow++)
{
GridViewRow rowToCompare = gridview.Rows[currentRow];
for (int otherRow = currentRow + 1; otherRow<gridview.Rows.Count; otherRow++)
{
GridViewRow row = gridview.Rows[otherRow];
bool duplicateRow = true;
//check Duplicate on Name Coloumn
if (((rowToCompare.Cells[1].Text) != (row.Cells[1].Text)) || ((rowToCompare.Cells[2].Text) != (row.Cells[2].Text)) || ((rowToCompare.Cells[3].Text) != (row.Cells[3].Text)))
{
duplicateRow = false;
}
else if (duplicateRow)
{
rowToCompare.BackColor = Color.Yellow;
row.BackColor = Color.Yellow;
}
}
}
}