Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete Duplicate entries in Datagrid / DataTable

Posted on 2004-09-01
6
Medium Priority
?
7,921 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

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

609 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