Remove (both) equal entries from DataTable

45 views Asked by At

I want to remove equal entries from DataTable. I tried DefaultView, but it only removes the equals and not all entries which including them.

DataView view = table1.DefaultView;
DataTable tbl = view.ToTable();
return tbl;
1

There are 1 answers

1
Haroon nasir On

you can do this

public DataTable RemoveDuplicate(DataTable dataTable, string columname)
{
  Hashtable hashTable = new Hashtable();
  List<String> duplicates = new List<String>();
 foreach (DataRow datarow in dataTable.Rows)
 {
   if (hashTable .Contains(datarow [columname]))
   {
    duplicateList.Add(datarow );
   }
   else
   {
    hashTable .Add(datarow [columname], string.Empty); 
   }
 }
  //Now remove the duplicates .
  foreach (DataRow datarow in duplicates )
  dataTable.Rows.Remove(datarow );
  return dataTable;
}