Showing posts with label remove duplicates from dataset/datatable.. Show all posts
Showing posts with label remove duplicates from dataset/datatable.. Show all posts

Friday, June 7, 2013

Remove duplicates from DataSet in .Net

da.Fill(ds,"Details");
DataTable dt = ds.Tables["Name"];
RemoveDuplicateRows(dt, "Name"); // Here Name is Column name of table
gvDetails.DataSource = ds;
gvDetails.DataBind();

// This method is used to delete duplicate rows of table
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();//hash table is used to store different types of data.It will take two parameters as input[Value and Name].
ArrayList duplicateList = new ArrayList();
foreach (DataRow dtRow in dTable.Rows)
{
if (hTable.Contains(dtRow[colName]))
duplicateList.Add(dtRow);
else
hTable.Add(dtRow[colName], string.Empty);
}
foreach (DataRow dtRow in duplicateList)
dTable.Rows.Remove(dtRow);
return dTable;
}