This time, I will show you how to generate Unique Data in GridView. This code is useful to avoiding data duplicacy in GridView:

HTML of page : EliminateGridview.aspx
<div> 
<h1> 
        Grid with duplicate data 
    </h1> 
<asp:GridView ID="gvDuplicate" runat="server" HeaderStyle-BackColor="gray" 
            AutoGenerateColumns="False" AlternatingRowStyle-BackColor="Yellow " BackColor="white" 
            BorderColor="blue" BorderStyle="None"> 
    <Columns> 
        <asp:BoundField DataField="Name" HeaderText="Name"> 
            <ItemStyle HorizontalAlign="Left" Width="20%" /> 
        </asp:BoundField> 
        <asp:BoundField DataField="City" HeaderText="City"> 
            <HeaderStyle Wrap="true"></HeaderStyle> 
            <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" /> 
        </asp:BoundField> 
    </Columns> 
</asp:GridView> 
</div> 
<div> 
<h1> 
        Grid with unique data 
    </h1> 
<asp:GridView ID="gvUnique" runat="server" HeaderStyle-BackColor="gray" AutoGenerateColumns="False" 
            AlternatingRowStyle-BackColor="Yellow " BackColor="white" BorderColor="blue" 
            BorderStyle="None"> 
    <Columns> 
        <asp:BoundField DataField="Name" HeaderText="Name"> 
            <ItemStyle HorizontalAlign="Left" Width="20%" /> 
        </asp:BoundField> 
        <asp:BoundField DataField="City" HeaderText="City"> 
            <HeaderStyle Wrap="true"></HeaderStyle> 
            <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" /> 
        </asp:BoundField> 
    </Columns> 
</asp:GridView> 
</div> 

Code behind page: EliminateGridview.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.Configuration; 

public partial class EliminateGridview: System.Web.UI.Page  

protected void Page_Load(object sender, EventArgs e)  

    if (!IsPostBack)  
    { 
        gvDuplicate.DataSource = gvUnique.DataSource = testData(); 
        gvDuplicate.DataBind(); 
        gvUnique.DataBind(); 
        RemoveDuplicateData(0); 
    } 


//generating datatable 
public DataTable testData()  

    DataTable dt = new DataTable(); 
    //Adding columns to datatable        
    dt.Columns.Add("Name"); 
    dt.Columns.Add("City"); 
    //Adding records to the datatable 
    dt.Rows.Add("Peter", "London"); 
    dt.Rows.Add("Peter", "Manchester"); 
    dt.Rows.Add("Peter", "Liverpool"); 
    dt.Rows.Add("Peter", "Bristol"); 
    dt.Rows.Add("Kevin", "Leeds"); 
    dt.Rows.Add("Kevin", "Glasgow"); 
    dt.Rows.Add("Kevin", "York"); 
    dt.Rows.Add("Anthony", "Cambridge"); 
    dt.Rows.Add("Anthony", "Bradford"); 
    dt.Rows.Add("Anthony", "Oxford"); 
    dt.Rows.Add("Steven", "Swansea"); 
    dt.Rows.Add("Richard", "Norwich"); 
    return dt; 

private void RemoveDuplicateData(int cellno)  

    string initialnamevalue = gvUnique.Rows[0].Cells[cellno].Text; 
    for (int i = 1; i < gvUnique.Rows.Count; i++) 
    { 

        if (gvUnique.Rows[i].Cells[cellno].Text == initialnamevalue)  
        { 
            gvUnique.Rows[i].Cells[cellno].Text = string.Empty; 
        }  
        else  
        { 
            initialnamevalue = gvUnique.Rows[i].Cells[cellno].Text; 
        } 
    } 


HostForLIFE.eu ASP.NET Core 1.0 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.