Removing Duplicate Records From DataTable.

public DataTable RemoveDuplicateRows(DataTable table, string DistinctColumn)
        ArrayList UniqueRecords = new ArrayList();
        ArrayList DuplicateRecords = new ArrayList();

        // Check if records is already added to UniqueRecords otherwise,
        // Add the records to DuplicateRecords
        foreach(DataRow dRow in table.Rows)
            if (UniqueRecords.Contains(dRow[DistinctColumn]))

        // Remove dupliate rows from DataTable added to DuplicateRecords
        foreach (DataRow dRow in DuplicateRecords)

        // Return the clean DataTable which contains unique records.
        return table;
    catch (Exception ex)
        return null;

DataTable DuplicateRecords = objDatabase.getTable("SQL Query");
DataTable UniqueRecords = RemoveDuplicateRows(DuplicateRecords,"Column Name to check for duplicate records");

By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s