?
Solved

Delete Duplicate entries in Datagrid / DataTable

Posted on 2004-09-01
6
Medium Priority
?
7,915 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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