Elimination of Data in GridView Using C# ASP.Net

We will learn here how to bind controls with data without a database.

This code is useful to avoid data delicacy in a GridView.

Also read:

Initial chamber

Step 1

Open your Visual Studio and create an empty website then provide a suitable name such as EliminateGridview.

Step 2

In Solution Explorer you will get your empty website, then add some web forms.

EliminateGridview. (your empty website). Right-click and select Add New Item Web Form. Name it EliminateGridview.aspx.

Design chamber

Step 3

Open the EliminateGridview.aspx file and write some code for the design of the application.
Choose the control from the toolbox and put it on your design page as in the following.

For duplicate data:

  1. <div>  
  2.     <h1>  
  3.         GridView with duplicate data value  
  4.     </h1>  
  5.     <asp:GridView ID="gvDuplicateData" runat="server" HeaderStyle-BackColor="gray"  
  6.             AutoGenerateColumns="False" AlternatingRowStyle-BackColor="Yellow " BackColor="white"  
  7.             BorderColor="blue" BorderStyle="None">  
  8.             <Columns>  
  9.                 <asp:BoundField DataField="Name" HeaderText="Name">  
  10.                     <ItemStyle HorizontalAlign="Left" Width="20%" />  
  11.                 </asp:BoundField>  
  12.                 <asp:BoundField DataField="City" HeaderText="City">  
  13.                     <HeaderStyle Wrap="true"></HeaderStyle>  
  14.                     <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" />  
  15.                 </asp:BoundField>  
  16.             </Columns>  
  17.         </asp:GridView>  
  18.   </div>  
To eliminate data:
  1. <div>  
  2.     <h1>  
  3.         GridView with unique data value  
  4.     </h1>  
  5.      
  6.         <asp:GridView ID="gvUniqueData" runat="server" HeaderStyle-BackColor="gray" AutoGenerateColumns="False"  
  7.             AlternatingRowStyle-BackColor="Yellow " BackColor="white" BorderColor="blue"  
  8.             BorderStyle="None">  
  9.             <Columns>  
  10.                 <asp:BoundField DataField="Name" HeaderText="Name">  
  11.                     <ItemStyle HorizontalAlign="Left" Width="20%" />  
  12.                 </asp:BoundField>  
  13.                 <asp:BoundField DataField="City" HeaderText="City">  
  14.                     <HeaderStyle Wrap="true"></HeaderStyle>  
  15.                     <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" />  
  16.                 </asp:BoundField>  
  17.             </Columns>  
  18.         </asp:GridView>  
  19.       
  20. </div>  
Design Page

HTML of page: EliminateGridview.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EliminateGridview.aspx.cs" Inherits="EliminateGridview" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>C-sharp corner article by Upendra Pratap Shahi</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <h1>  
  13.             GridView with duplicate data value  
  14.         </h1>  
  15.         <asp:GridView ID="gvDuplicateData" runat="server" HeaderStyle-BackColor="gray"  
  16.                 AutoGenerateColumns="False" AlternatingRowStyle-BackColor="Yellow " BackColor="white"  
  17.                 BorderColor="blue" BorderStyle="None">  
  18.                 <Columns>  
  19.                     <asp:BoundField DataField="Name" HeaderText="Name">  
  20.                         <ItemStyle HorizontalAlign="Left" Width="20%" />  
  21.                     </asp:BoundField>  
  22.                     <asp:BoundField DataField="City" HeaderText="City">  
  23.                         <HeaderStyle Wrap="true"></HeaderStyle>  
  24.                         <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" />  
  25.                     </asp:BoundField>  
  26.                 </Columns>  
  27.             </asp:GridView>  
  28.     </div>  
  29.         <div>  
  30.         <h1>  
  31.             GridView with unique data value  
  32.         </h1>  
  33.          
  34.             <asp:GridView ID="gvUniqueData" runat="server" HeaderStyle-BackColor="gray" AutoGenerateColumns="False"  
  35.                 AlternatingRowStyle-BackColor="Yellow " BackColor="white" BorderColor="blue"  
  36.                 BorderStyle="None">  
  37.                 <Columns>  
  38.                     <asp:BoundField DataField="Name" HeaderText="Name">  
  39.                         <ItemStyle HorizontalAlign="Left" Width="20%" />  
  40.                     </asp:BoundField>  
  41.                     <asp:BoundField DataField="City" HeaderText="City">  
  42.                         <HeaderStyle Wrap="true"></HeaderStyle>  
  43.                         <ItemStyle HorizontalAlign="Left" Width="20%" Wrap="true" />  
  44.                     </asp:BoundField>  
  45.                 </Columns>  
  46.             </asp:GridView>  
  47.           
  48.     </div>  
  49.     </form>  
  50. </body>  
  51. </html>  
Your design looks as in the following:

design

Code chamber

Step 4

In the code chamber we will write some code so that our application works.

Add the following namespaces in the namespace section of your code behind page as in the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
Now your page looks as in the following.

Code behind Page

EliminateGridview.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9.   
  10. public partial class EliminateGridview : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!IsPostBack)  
  15.         {  
  16.   
  17.   
  18.             gvDuplicateData.DataSource = gvUniqueData.DataSource = testData();  
  19.   
  20.             gvDuplicateData.DataBind();  
  21.             gvUniqueData.DataBind();  
  22.   
  23.             RemoveDuplicateData(0);  
  24.         }  
  25.     }  
  26.   
  27.     public DataTable testData() {  
  28.   
  29.         DataTable dt = new DataTable();  
  30.   
  31.         //Adding columns to datatable          
  32.         dt.Columns.Add("Name");  
  33.         dt.Columns.Add("City");  
  34.   
  35.         //Adding records to the datatable  
  36.         dt.Rows.Add("Upendra""Gorakhpur");  
  37.         dt.Rows.Add("Upendra""Delhi");  
  38.         dt.Rows.Add("Upendra""Bangaluru");  
  39.         dt.Rows.Add("Upendra""Lucknow");  
  40.         dt.Rows.Add("Vishal""Allahabad");  
  41.         dt.Rows.Add("Vishal""Gorakhpur");  
  42.         dt.Rows.Add("Vishal""Delhi");  
  43.         dt.Rows.Add("Ranjan""Odisha");  
  44.         dt.Rows.Add("Ranjan""Mumbai");  
  45.         dt.Rows.Add("Ranjan""Noida");  
  46.         dt.Rows.Add("Ashish""Lucknow");  
  47.         dt.Rows.Add("Gitendra""Noida");  
  48.   
  49.         return dt;  
  50.     }  
  51.     private void RemoveDuplicateData(int cellno)  
  52.     {  
  53.         string initialnamevalue = gvUniqueData.Rows[0].Cells[cellno].Text;  
  54.         for (int i = 1; i < gvUniqueData.Rows.Count; i++)  
  55.         {  
  56.   
  57.             if (gvUniqueData.Rows[i].Cells[cellno].Text == initialnamevalue)  
  58.             {  
  59.                 gvUniqueData.Rows[i].Cells[cellno].Text = string.Empty;  
  60.             }  
  61.             else  
  62.             {  
  63.                 initialnamevalue = gvUniqueData.Rows[i].Cells[cellno].Text;  
  64.             }  
  65.         }  
  66.     }  
  67. }  
Output

Grid with duplicate data

I hope you liked this. Have a good day. Thank you for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all