Saturday 23 August 2014

Highlight duplicate rows in gridview in asp.net

Highlight Duplicate rows in Gridview
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.



Fancy Image Slideshow of Image in Jquery Beautiful Slideshow in Jquery 

Awesome Login Popup in Jquery Signup and Login Popup in Jquery

 Default3.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Highlight duplicate Rows in Gridview in asp.net</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:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            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] = "pintu";
        dr1[2] = "Ahmedabad";
        dr1[3] = "2584487464";
        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2[0] = "2";
        dr2[1] = "kirit";
        dr2[2] = "Ahmedabad";
        dr2[3] = "985218451";
        dt.Rows.Add(dr2);

        DataRow dr3 = dt.NewRow();
        dr3[0] = "3";
        dr3[1] = "kirit";
        dr3[2] = "Ahmedabad";
        dr3[3] = "985218451";
        dt.Rows.Add(dr3);

        DataRow dr4 = dt.NewRow();
        dr4[0] = "4";
        dr4[1] = "rahul";
        dr4[2] = "Rajkot";
        dr4[3] = "955487441";
        dt.Rows.Add(dr4);

        DataRow dr5 = dt.NewRow();
        dr5[0] = "5";
        dr5[1] = "kirit";
        dr5[2] = "Ahmedabad";
        dr5[3] = "985218451";
        dt.Rows.Add(dr5);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        HighlightDuplicate(GridView1);
    }
    public void HighlightDuplicate(GridView gridview)
    {
        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))
                {
                    duplicateRow = false;
                }
                else if (duplicateRow)
                {
                    rowToCompare.BackColor = Color.Yellow;
                    row.BackColor = Color.Yellow;
                }
            }
        }
    }

}

0 comments:

Post a Comment