Click here to Skip to main content
15,881,281 members
Articles / Web Development / ASP.NET

Eliminate Duplicate Values from the Grid View

Rate me:
Please Sign up or sign in to vote.
4.32/5 (14 votes)
24 May 2009CPOL1 min read 93.2K   1.9K   19   7
This example will demonstrate how to eliminate duplicate values from the Grid View
EliminateDuplicateValuesFromtheGrid

Introduction

Let’s start with an example. 

Suppose a person Mr. XYZ has purchased two or three houses in three different places. Another person Mr. ABC has more than one house. Now the requirement is to display the information of those persons along with the house addresses with the constraint that the names should not be repeated. This example will demonstrate how to resolve such problems.

Straight to the Program

In the program, I have a data source (which is a datatable) as shown below:

C#
DataTable dtsource = new DataTable();

//Adding columns to datatable        
dtsource.Columns.Add("Name");        
dtsource.Columns.Add("Address");        

//Adding records to the datatable
dtsource.Rows.Add( "Name1", "Address11");
dtsource.Rows.Add("Name1", "Address12");
dtsource.Rows.Add("Name1", "Address13");
dtsource.Rows.Add("Name2", "Address21");
dtsource.Rows.Add("Name2", "Address22");
dtsource.Rows.Add("Name3", "Address31");
dtsource.Rows.Add("Name3", "Address32");
dtsource.Rows.Add("Name4", "Address41");
dtsource.Rows.Add("Name4", "Address42");	

As can be observed that the names are repeated, nevertheless, the addresses are unique. I have taken two GridViews, the first which will show the information having duplicate values while the second is without duplicates. In the pageload event, I am binding the respective grids with the datasource Next, the task is to remove the duplicate rows. A small two step algorithm is running behind that:

Algorithm: Remove duplicate data 
Step 1: Store the first row's , first cells content

Step 2: Loop through the entire grid from the 2nd row till the end 
	and check if the Name column's value is same or not.
	If it is same, then replace the value with empty string 
	else continue with the new value. The above process will repeat itself

So let’s portray the above algorithm into the picture.

C#
/// <summary>
/// Function name: GenerateUniqueData
/// Purpose: Eliminates duplicate record from a particular cell
/// </summary>
///
 private void GenerateUniqueData(int cellno)
 {
     //Logic for unique names

     //Step 1:

     string initialnamevalue = grdUniqueNames.Rows[0].Cells[cellno].Text;

     //Step 2:

     for (int i = 1; i < grdUniqueNames.Rows.Count; i++)
     {

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

Function GenerateUniqueData does the necessary function. It accepts only one parameter, CellNo which is used to determine upon which cell, the algorithm needs to work. The calling function GenerateUniqueData(0) passes the values as 0 indicating that the first column’s data should be unique.

Conclusion

Though simple and not needing much explanation, since it’s a real time scenario, I thought of sharing this small and simple example with others.

History

  • 24th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 1267486215-Mar-18 20:13
Member 1267486215-Mar-18 20:13 
QuestionEliminate Duplicate value in Gridview Pin
Prosenjit dass15-Jun-12 0:28
Prosenjit dass15-Jun-12 0:28 
QuestionThank you Pin
alarhby7-Jun-12 12:24
alarhby7-Jun-12 12:24 
QuestionGridview rows Color Pin
Member 859218024-Feb-12 23:14
Member 859218024-Feb-12 23:14 
QuestionGridview Avoid duplicate values in columns Pin
Member 859218023-Feb-12 22:09
Member 859218023-Feb-12 22:09 
GeneralDistinct, outer join.. Pin
Muammar©30-Jun-09 8:10
Muammar©30-Jun-09 8:10 
GeneralGood work Pin
rameshmutyala24-May-09 21:21
rameshmutyala24-May-09 21:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.