Wednesday 9 October 2013

Remove duplicate rows from a datatable in ASP.NET/C#



In this article I will explain how to remove (delete) duplicate rows (records) from DataTable using DataView in C# .

Namespaces
You will need to import the following namespace.

using System.Data;

Code:C#

DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "Raja Durai", "United States");
        dt.Rows.Add(2, "Raja", "United States");
        dt.Rows.Add(3, "Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
dt = dt.DefaultView.ToTable(true, "Id", "Name", "Country");

No comments:

Post a Comment