Click here to Skip to main content
15,121,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I really need some help here.
I have a ListView where I would like to remove duplcate rows.
The criteria :
item.text and subItem

Like -
if myItem.Text = myItem.Text And myItem.SubItem = myItem.SubItem then
Remove the row

Thank you in advance,
Henrik
Posted

1 solution

And what's your doubt about?
Iterating ((wisely: you are removing items) on all the ListView items you could use the Find[^] for getting the array of items matching the current one and, interating in turn on such array, remove the ones matching the subitem too.

[update 2]
Try (I've not tested it):
Dim i as Integer = 0

VB
While (i < lstV_Result.Items.Count) 
      RemoveListViewLine(i, lstV_Result.Items(i).SubItems(1).Text, lstV_Result.Items(i).SubItems(9).Text)
      i = i + 1
End While


Private Sub RemoveListViewLine(ByVal n as Integer, ByVal TextCrit As String, ByVal SubCrit As String)
     Dim li As ListViewItem
     n = n + 1
     While (n < lstV_Result.Items.Count)
          li = lstV_Result.Items(n)
          If li.SubItems(1).Text = TextCrit And li.SubItems(9).Text = SubCrit Then
              lstV_Result.Items.Remove(li)
          Else
              n = n + 1
          End If
     End While
End Sub

[/update 2]
   
v4
Comments
Maciej Los 26-Sep-13 7:24am
   
Short and to the point!
5ed!
CPallini 26-Sep-13 7:33am
   
Thank you.
N. Henrik Lauridsen 27-Sep-13 7:39am
   
Hi Again,
Thank you for your solution. I am sorry to bother you with this but really would appreciate your help.
I Work untill it reaches an index 34 and stop with an error message. I guess maybe because it asumes the listview still contain the original number of items. There are 94 rows at the begining. 73 should be removed. If I use For i = 0 to 33 it Works fine and removes the duplets. When I use For i = 0 to lstV_Result.Items.Count - 1 I get an error : System.ArgumentOutOfRangeException: InvalidArgument = value '34' illigal for index
CPallini 27-Sep-13 7:53am
   
That happens because Visual Basic's (unlike, for instance C#'s) For evaluates its end limit once, before the first iteration happens. Use While, instead.
I tried to fix my code, however, it is still not tested.
N. Henrik Lauridsen 27-Sep-13 8:43am
   
Thank you for your solution. Problem solved.
CPallini 27-Sep-13 9:04am
   
You are welcome.
N. Henrik Lauridsen 26-Sep-13 7:53am
   
Thank you CPallini for your fast reply.
I am sorry. I am to much of a newbie to quite understand.
I have a ListView filled with data read from files where some are identical.
I have made a loop which goes from 0 to ListView.Items.Count -1
but as you said I am trying to remove all the ListView Items.

For i = 0 To lstV_Result.Items.Count - 1
RemoveListViewLine(lstV_Result.Items(i).SubItems(1).Text, lstV_Result.Items(i).SubItems(9).Text)
Next

Private Sub RemoveListViewLine(ByVal TextCrit As String, ByVal SubCrit As String)
Dim li As ListViewItem
For Each li In lstV_Result.Items
If li.SubItems(1).Text = TextCrit And li.SubItems(9).Text = SubCrit Then
lstV_Result.Items.Remove(li)
End If
Next
End Sub
Member 11268443 3-Dec-14 4:31am
   
Thank You so much, your code helped me to improve my project..keep continue to share this kind of interesting topic....!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900