Reputation: 11
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;
Upvotes: 0
Views: 44
Reputation: 676
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;
}
Upvotes: 0