Dim hTable As Hashtable = New Hashtable()
        Dim duplicateList As ArrayList = New ArrayList()
        Dim itm As ListViewItem
        For Each itm In ListView1.Items
            If hTable.ContainsKey(itm.Text) AndAlso hTable.ContainsKey(itm.SubItems(1).Text) AndAlso hTable.ContainsKey(itm.SubItems(2).Text) AndAlso hTable.ContainsKey(itm.SubItems(3).Text) Then 'duplicate
                duplicateList.Add(itm)
            Else
                hTable.Add(itm.Text, String.Empty)
            End If
        Next
        'remove duplicates
        For Each itm In duplicateList
            ListView1.Items.Remove(itm)
        Next

I want to compare 3 subitem in listview like this:
Date____________Name_______________Value
11/12/13________llallala_______________12334
11/12/13________llallala_______________12334
11/12/13________ddsfvxcv_____________16542
13/12/12________ddsfvxcv_____________12334
10/12/13________owowewe_____________23024

then normally it will remove first and second row because it is duplicate but now it cannot....
I means it just compare between first column....so if the first column is same but second and third is different then it will remove wrong....
remove item code is working just the compare code not working
sry for bad english

Recommended Answers

All 3 Replies

Using a custom equalitycomparer and LINQ should do the job:

    Dim hTable As List(Of ListViewItem) = (From lvi In ListView1.Items
                                   Let newlvi = DirectCast(lvi, ListViewItem)
                                   Select newlvi
                                   ).Distinct(New CompareLVI).ToList
    ListView1.Items.Clear()
    ListView1.Items.AddRange(hTable.ToArray)

Public Class CompareLVI
    Implements IEqualityComparer(Of ListViewItem)

    Public Function Equals1(x As ListViewItem, y As ListViewItem) As Boolean Implements IEqualityComparer(Of ListViewItem).Equals
        Return x.Text = y.Text AndAlso x.SubItems(1).Text = y.SubItems(1).Text
    End Function

    Public Function GetHashCode1(obj As ListViewItem) As Integer Implements IEqualityComparer(Of ListViewItem).GetHashCode

        Return obj.Text.GetHashCode Xor obj.SubItems(1).Text.GetHashCode
    End Function
End Class

thx the code is working i added in some code for compare more than 1 column:

 Return x.Text = y.Text AndAlso x.SubItems(1).Text = y.SubItems(1).Text AndAlso x.SubItems(2).Text = y.SubItems(2).Text AndAlso x.SubItems(3).Text = y.SubItems(3).Text

 Return obj.Text.GetHashCode Xor obj.SubItems(1).Text.GetHashCode
        Return obj.Text.GetHashCode Xor obj.SubItems(2).Text.GetHashCode
        Return obj.Text.GetHashCode Xor obj.SubItems(3).Text.GetHashCode

just the listview just refresh.....

Please remember to mark this solved thanks.

Be a part of the DaniWeb community

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