Click here to Skip to main content
11,709,375 members (62,342 online)
Click here to Skip to main content

Eliminate Duplicate Values from the Grid View

, 24 May 2009 CPOL 48.6K 1.4K 19
Rate this:
Please Sign up or sign in to vote.
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:

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.

/// <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)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
QuestionEliminate Duplicate value in Gridview Pin
Prosenjit dass15-Jun-12 0:28
memberProsenjit dass15-Jun-12 0:28 
QuestionThank you Pin
alarhby7-Jun-12 12:24
memberalarhby7-Jun-12 12:24 
QuestionGridview rows Color Pin
Member 859218024-Feb-12 23:14
memberMember 859218024-Feb-12 23:14 
QuestionGridview Avoid duplicate values in columns Pin
Member 859218023-Feb-12 22:09
memberMember 859218023-Feb-12 22:09 
GeneralDistinct, outer join.. Pin
Muammar©30-Jun-09 8:10
memberMuammar©30-Jun-09 8:10 
GeneralGood work Pin
rameshmutyala24-May-09 21:21
memberrameshmutyala24-May-09 21:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 24 May 2009
Article Copyright 2009 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid