Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Remove Duplicates In DataTable
Author:
Gehan Fernando
E-mail:
Click to e-mail author
Submitted:
2/16/2012
Version:
C#
Compatibility:
C#
Category:
Databases
Views:
11216
Remove Duplicates Data-table. Using this code sample you can remove thousands of duplicate data in less than 3 seconds.
Declarations:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms;
Code:
namespace Remove.Duplicates { public partial class Form1 : Form { // Developer Gehan Fernando // Date :- 17-02-2012 // Application :- Remove Duplicate data in data-table public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { DataTable table = new DataTable("Customers"); DataColumn col = null; col = new DataColumn("CustomerID", Type.GetType("System.Int32")); table.Columns.Add(col); col = new DataColumn("CustomerName", Type.GetType("System.String")); table.Columns.Add(col); DataRow row = null; row = table.NewRow(); row["CustomerID"] = 1; row["CustomerName"] = "Gehan Fernando"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 2; row["CustomerName"] = "Charith Fernando"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 1; row["CustomerName"] = "Gehan Fernando"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 3; row["CustomerName"] = "Saman Perera"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 4; row["CustomerName"] = "Nuwan Prasad"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 1; row["CustomerName"] = "Gehan Fernando"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 3; row["CustomerName"] = "Saman Perera"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 3; row["CustomerName"] = "Saman Perera"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 3; row["CustomerName"] = "Saman Perera"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 3; row["CustomerName"] = "Saman Perera"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 4; row["CustomerName"] = "Nuwan Prasad"; table.Rows.Add(row); row = table.NewRow(); row["CustomerID"] = 5; row["CustomerName"] = "Janak Nuwan"; table.Rows.Add(row); dataGridView1.DataSource = table; dataGridView2.DataSource = RemoveDuplicates(table, "CustomerID"); } catch (Exception ex) { throw ex; } } private DataTable RemoveDuplicates(DataTable duplicates, String columnName) { SortedDictionary
unique = new SortedDictionary
(); DataTable result = duplicates.Copy(); String queryvalue = String.Empty; foreach (DataRow row in duplicates.Rows) { var query = (from l in unique where l.Key == row[columnName].ToString().Trim() select l).ToList(); if (query.Count == 0) unique.Add(row[columnName].ToString().Trim(), row); else { queryvalue = String.Empty; queryvalue = String.Format("{0} = {1}", columnName, row[columnName].ToString().Trim()); DataRow[] rows = result.Select(queryvalue); if (rows.Length > 0) for (int i = rows.Length - 1; i >= 1; i += -1) result.Rows.Remove(rows[i]); } } return result; } } }
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement