65.9K
CodeProject is changing. Read more.
Home

Eliminate Duplicate Values from the Grid View

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.32/5 (14 votes)

May 24, 2009

CPOL

1 min read

viewsIcon

94189

downloadIcon

1932

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