Click here to Skip to main content
15,896,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello in my application i retriving the data from database in to the data table here i want to delete the previous row from the data table.

What I have tried:

suppose my row are following 1st row(1,john,2000) 2nd row (2,marry,21000), 3rd (1,john,22000) here i want to delete the duplicate record of john means i want to delete the first row and display the 3 row .
Posted
Updated 3-Nov-16 17:40pm
Comments
ZurdoDev 2-Nov-16 6:59am    
Can you change what is filling the DataTable to not return the duplicates? That would be the better way.
[no name] 2-Nov-16 7:00am    
Why don't you just query the distinct data from the database?

Remove duplicate using following code

C#
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

   //Removing a list of duplicate items from datatable.
   foreach (DataRow dRow in duplicateList)
      dTable.Rows.Remove(dRow);

   //Datatable which contains unique records will be return as output.
      return dTable;
}
 
Share this answer
 
check this thread c# - Best way to remove duplicate entries from a data table - Stack Overflow[^] it has different solution, choose the one you need.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900