i have a list of record that already populated but my problems is How to find out if there are any duplicates? VB.NET

Recommended Answers

It will be easy to do the same at database level.

Jump to Post

All 3 Replies

It will be easy to do the same at database level.

Member Avatar

If you just want to know if there are duplicate items in the listview you can use this function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   Dim ListView As New ListView
   Dim ListViewItem As ListViewItem

   For i = 1 To 3
      ListViewItem = ListView.Items.Add("Item")

   If ListViewContainsDuplicates(ListView, 0) Then
       MsgBox("SubItem 0 has duplicate values")
   End If

   If ListViewContainsDuplicates(ListView, 1) Then
      'will not be called.
      MsgBox("SubItem 1 has duplicate values")
   End If

End Sub

Private Function ListViewContainsDuplicates(ByVal listView As ListView, ByVal subItemIndex As Integer) As Boolean

   'Get a distinct count of the subitem 
   Dim DistinctCount As Integer = (From Item In listView.Items _
                                   Select CType(Item, ListViewItem).SubItems(subItemIndex).Text _

   'If the distinct count is not equal to the total listview count
   'then there are duplicate values.
    Return DistinctCount <> listView.Items.Count

End Function

Hi, Unhnd Exception nice It works... Thank you for your very appreciate efforts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.