Is there any way, to delete a single row in ListView?
Printable View
Is there any way, to delete a single row in ListView?
Delete the row from the data source from which the listview was loaded, clear, and reload the listview.
ListView1.Items.RemoveAt(index)
Noted....... But is there a way to be able to delete the selected index....???
Assuming you do not allow MultiSelect, there will only be one selected item so;
If ListView1.SelectedItems.Count > 0 Then
ListView1.Items.RemoveAt(ListView1.SelectedIndices(0))
End If
And if by any chance i had allowed MultiSelect...?
For Each lvi As ListViewItem In ListView1.SelectedItems
lvi.Remove()
Next
You could use this whether you have MultiSelect or not.
loop backwards through the collection, removing the selected items.
if you don't loop backwards, as soon as you remove an element from the collection, and try to move forwards, you will get an error.Code:For i As Integer = (ListView1.Items.Count - 1) To 0 Step -1
If ListView1.Items(i).Selected Then
ListView1.Items.RemoveAt(i)
End If
Next
this will not work, for the reasons stated in my above post.Quote:
Originally Posted by Bulldog
EDIT: sorry, my bad... i thought you were looping 'items'... you can do this loop on selected items.
Both of them work perfectly! Thanks!
But i have one more question...: I have the items of the LV loaded from a text file. I want to delete the file when the selected item is removed from the LV...
If I have "OMG.txt" and in the ListView I have:
111 OMG 111 *In different Columns*
How can i delete that file?
so that would mean to say that column index 1 (second column) would have the name of the file, minus the .txt part?
I think it is also important to know the actual path to the file, and not just its name. Do these files sit in the same directory as the exe?
No probs. :DQuote:
Originally Posted by kleinma
Yeah, the column index 1 would have the file name minus the .txt part. The path of the file would be the same as the .exe
try this:
vb Code:
For Each lvi As ListViewItem In ListView1.SelectedItems IO.File.Delete(lvi.SubItems(1).Text & ".txt") lvi.Remove() Next
That works great! Thanks a lot! =D