Highlight duplicate rows in gridview in asp.net


Description:-
In this example we explain that how to Highlight duplicate row in Gridview in asp.net or display duplicate rows with highlight color or different background row in GridView in asp.net. Generally we highlight row so user can easily look and feel that and understand that what to do next.

Generally that is great functionality if you have requirement to delete duplicate record then you do not have to find it one by one manually but you can use this functionality and easily delete highlighted rows.

Default3.aspx:-

<!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>

Default3.aspx.cs:-

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;
      }
    }
  }
}

Related Posts

Previous
Next Post »

3 comments

comments
January 11, 2018 at 12:30:00 PM GMT+5:30 delete

Thanks for posting your valuable thoughts with us & our readers. Please keep continue writing on this blog.
Bulk SMS Service Provider

Reply
avatar
November 17, 2021 at 12:10:00 PM GMT+5:30 delete

Thanks for If you want reach your customer easily just go for bulk sms api, we provides different kind of Bulk SMS services Without DLT | Free SMS API | Free Registration,
bulk sms india is one of the most effective, reliable and cost effective ways to establish your businesses. It allows you to get a higher income and get a customer easily. Rat SMS is the most popular leading company who provides the best promotional sms provider Services.




You can check with also bulk sms india and Otp SMS

Reply
avatar
April 20, 2022 at 6:28:00 PM GMT+5:30 delete

Excellent article and with lots of information. I really learned a lot here. Do share more like this.
Blue Prism Tool
Blue Prism Automation

Reply
avatar

Thanks for comments.....