• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7923
  • Last Modified:

Delete Duplicate entries in Datagrid / DataTable

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
auk_ie
Asked:
auk_ie
  • 4
  • 2
1 Solution
 
eternal_21Commented:
You could put a restraint on the DataTable such that duplicate rows cannot be entered in the first place... would that work?
0
 
eternal_21Commented:
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
 
eternal_21Commented:
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.

 
auk_ieAuthor Commented:
Nice let look into both these solutions, I will reply tommorow
0
 
eternal_21Commented:
How did that work for you auk_ie?
0
 
auk_ieAuthor Commented:
I actually got distracted for a bit, But I did get a chance to try out the first function, very cool
0

Featured Post

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.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now