Solved

Delete Duplicate entries in Datagrid / DataTable

Posted on 2004-09-01
6
7,872 Views
Last Modified: 2011-04-14
Hello dataGrid1 is bound to Products dataTable1

The format of the table is something like this after the user enters data into the dataGrid1 fields

colCategory   colDecription  colStyleNo
Top              Summer        123A
Blouse          Winter           456A
Top              Summer        123A

I need a function that will detect if the user has entered any duplicate entries (in this case the first and last entry are the same)?

How can I do this?
0
Comment
Question by:auk_ie
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 11958804
You could put a restraint on the DataTable such that duplicate rows cannot be entered in the first place... would that work?
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11958856
If that works, you can achieve that by sending your DataTable to this method:

  static void AddUniqueRowConstraint(System.Data.DataTable dataTable)
  {
    System.Data.DataColumn[] tableColumns = new System.Data.DataColumn[dataTable.Columns.Count];
    dataTable.Columns.CopyTo(tableColumns, 0);
    dataTable.Constraints.Add("NoDuplicateRows", tableColumns, false);
  }

Duplicate rows will not be possible!
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 11960088
If that doesn't solve your problem, here are two functions.  The first will report true/false if the DataTable has duplicate records, the second will remove any duplicates from the Row collection:

    bool HasDuplicates(System.Data.DataTable dataTable)
    {
      System.Data.DataTable duplicateTable = dataTable.Clone();
      System.Data.DataColumn[] primaryKey = new System.Data.DataColumn[duplicateTable.Columns.Count];
      duplicateTable.Columns.CopyTo(primaryKey, 0);
      duplicateTable.PrimaryKey = primaryKey;

      foreach(System.Data.DataRow dataRow in dataTable.Rows)
        if(duplicateTable.Rows.Contains(dataRow.ItemArray))
          return true;
        else
          duplicateTable.Rows.Add(dataRow.ItemArray);
     
      return false;
    }

    void RemoveDuplicates(System.Data.DataTable dataTable)
    {
      System.Data.DataTable duplicateTable = dataTable.Clone();
      System.Data.DataColumn[] primaryKey = new System.Data.DataColumn[duplicateTable.Columns.Count];
      duplicateTable.Columns.CopyTo(primaryKey, 0);
      duplicateTable.PrimaryKey = primaryKey;
     
      System.Data.DataRow[] dataRows = new System.Data.DataRow[dataTable.Rows.Count];
      dataTable.Rows.CopyTo(dataRows, 0);
      foreach(System.Data.DataRow dataRow in dataRows)
        if(duplicateTable.Rows.Contains(dataRow.ItemArray))
          dataTable.Rows.Remove(dataRow);
        else
          duplicateTable.Rows.Add(dataRow.ItemArray);
    }
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:auk_ie
ID: 11960326
Nice let look into both these solutions, I will reply tommorow
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12011409
How did that work for you auk_ie?
0
 

Author Comment

by:auk_ie
ID: 12011440
I actually got distracted for a bit, But I did get a chance to try out the first function, very cool
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now