GridView Display Duplicate Row Values or HighLight Duplicate Row Values Using Database in Asp.Net C#

GridView Display Duplicate Values or HighLight Duplicate Values


GridView Display Duplicate Row OR Highlight Row by Color With SQL Database in Asp.Net C#.

                        Download Coding

                                            Download

                                   DEMO



HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" align="center" runat="server">
        <div align="center"><asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Show Duplicate Row Total" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="109px"  Width="267px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" />
                <asp:BoundField DataField="name" HeaderText="Name" />
               <asp:BoundField DataField="country" HeaderText="Country" />
            </Columns>
            <HeaderStyle BackColor="#666666" />
        </asp:GridView>
    </form>
</body>
</html>

C# CODING


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;

public partial class BindDropDown : System.Web.UI.Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    for (int ThisRow = 0; ThisRow < GridView1.Rows.Count - 1; ThisRow++)
        {
            GridViewRow CompareRow = GridView1.Rows[ThisRow];
            for (int NextRow = ThisRow + 1; NextRow < GridView1.Rows.Count; NextRow++)
            {
                GridViewRow row = GridView1.Rows[NextRow];
                bool DuplicateRow = true;
                if ((CompareRow.Cells[1].Text) == (row.Cells[1].Text))
                {
                    row.BackColor = System.Drawing.Color.Red;
                    CompareRow.BackColor = System.Drawing.Color.Red;                                     
                }
                else if (DuplicateRow)
                {
                    DuplicateRow = false;
                }
            }
        }
    }  
}


First - Add New WebForm - Select GridView,Button 

From ToolBox





Next - Add Gridview - Edit Column - Add BoundField - Change Header Text  & Data For All 





Next - Select GridView - Property[F4] - Event - Double Click - RowDataBound





Next - Add Select Coding For Button Event - Row DataDound Add Check Duplicate






Next - Run[F5] - Show Duplicate










0 comments:

Post a Comment