Click here to Skip to main content
Licence CPOL
First Posted 24 May 2009
Views 17,798
Downloads 383
Bookmarked 16 times

Eliminate Duplicate Values from the Grid View

By Niladri_Biswas | 24 May 2009
This example will demonstrate how to eliminate duplicate values from the Grid View

1

2
1 vote, 12.5%
3
1 vote, 12.5%
4
6 votes, 75.0%
5
4.25/5 - 8 votes
μ 4.25, σa 1.30 [?]
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)

About the Author

Niladri_Biswas

Software Developer (Senior)
Software industry
India India

Member
Lead Engineer at HCL Technologies Ltd.
Code Project MVP 2012

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDistinct, outer join.. PinmemberMuammar©9:10 30 Jun '09  
GeneralGood work Pinmemberrameshmutyala22:21 24 May '09  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 24 May 2009
Article Copyright 2009 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid