Click here to Skip to main content
15,895,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, am NanaKwame.
Please I want to delete a single row or multiple rows from a listview control using a delete button. If I select the first row is able to delete the selected row from the listview, but if I select second row or any other row apart from the first row is not able to delete the selected row but rather delete the first row that is the first index number which is zero(0) in the listview control.

Listview is the name of the listview control I used.
Any assistance will be of great help to me. Thanks in advance.
Below is the code I used.

VB
Dim x As Integer
       For x = 0 To ListView.SelectedIndices.Count - 1
           Me.ListView.Items.RemoveAt(x)
       Next
Posted
Comments
[no name] 27-Jul-13 21:54pm    
Okay. Let's see if I can explain this simply. The reason that you are deleting the first row (0) when the selected row is something other than the first row is because you are using x as the index of the row to delete. What is the value of x? The value of x is 0 the first time. To delete the selected row from your listview, you would need to use the indexes contained in the ListView.SelectedIndices property, NOT x. See http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindices.aspx

1 solution

You Might be looking for this.

VB
For Each x As Windows.Forms.ListViewItem In ListView1.Items
    If x.Selected Then
        x.Remove()

    End If
Next
 
Share this answer
 
Comments
NanaKwame 28-Jul-13 18:38pm    
I have tried the code you provided and it has solve the problem for me.
Thanks. I appreciate it.
Mr.TMG 28-Jul-13 19:00pm    
Glad to help.
Mr.TMG 28-Jul-13 19:10pm    
And please remember to click the accept Solution button so that people will know that it's solved.

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