Delete Duplicate rows of a DataTable by C#

Thursday, July 19, 2012

Some time when you bind gridview ,you can find some duplicate rows in that grid, by this underlying piece of code you can easily avoid (or delete ) duplicate rows to bind  with that gridview :

// This method is used to delete duplicate rows of table
    public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
    {
        Hashtable hTable = new Hashtable();
        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;
    }

0 comments:

Post a Comment