Solved

Delete Duplicate entries in Datagrid / DataTable

Posted on 2004-09-01
6
7,898 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

737 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