How to identify duplicate values in gridView

1

I am loading a GridView with several records from the form, a field is E-mail, at the end of the process I send an email to the addresses added in that column. The problem is that the E-mail field is usually repeated, so the mail is sent as many times as it is repeated in the GridView to the same recipient.

I'm trying to avoid it this way but it does not work:

 For Each row As GridViewRow In GridView1.Rows
        If row.Cells(0).Text = row.Cells(0).Text Then
            'Duplicado
        Else
            'Enviar Correo
        End If
    Next
    
asked by yulfredy rodriguez 22.07.2017 в 19:23
source

1 answer

0

I can think of two ways:

Using a list

You scroll through your GridView records by adding each email to a new list. Once your list is full you order it in alphabetical order (you can do that with the method Sort ).

Thus the duplicate records appear contiguously and it will be easy to identify them. To send the emails you go through the list verifying that the current item is different from the previous one, otherwise it means that it is duplicated.

Using HashSet

HashSet is a structure specially designed to handle collections of objects without repetitions.

You create an instance of HashSet. You go through the records of your GridView and add them to the HashSet. At the end of the HashSet it will contain all the emails only once. It will be enough to scroll through its elements to send the emails.

    
answered by 22.07.2017 в 22:21