Reputation: 1054
I have a listview which is bound to a listviewitem as you will see below - problem is I need to clear out the database on a timer and start from scratch,
I know to clear the listview I just do
listview.items.clear()
problem is I need to also clear the listviewitem or it is going to rebind the same information to the listview.
declared:
Dim TempStr(7) As String
Dim TempNode As ListViewItem
problematic statement:
listview2.items.clear()
For pop As Integer = 0 To pendrun.Rows.Count - 1
TempStr(0) = pendrun.Rows(pop)("RID")
Select Case pendrun.Rows(pop)("Utype")
Case 1
TempStr(1) = "BLS Ambulance"
Case 2
TempStr(1) = "ALS Ambulance"
Case 3
TempStr(1) = "SCT Ambulance"
Case 4
TempStr(1) = "Wheelchair Van"
Case 5
TempStr(1) = "Taxi"
End Select
Select Case pendrun.Rows(pop)("Curgency")
Case 1
TempStr(2) = "Scheduled"
Case 2
TempStr(2) = "Non-Scheduled"
Case 3
TempStr(2) = "ASAP"
Case 4
TempStr(2) = "STAT"
End Select
TempStr(3) = pendrun.Rows(pop)("Pname")
TempStr(4) = pendrun.Rows(pop)("Texttime")
TempStr(5) = pendrun.Rows(pop)("sname") & " - " & pendrun.Rows(pop)("sfaddress") & ", " & pendrun.Rows(pop)("sfcity") & ", " & pendrun.Rows(pop)("sfstate")
TempStr(6) = pendrun.Rows(pop)("dname") & " - " & pendrun.Rows(pop)("dfaddress") & ", " & pendrun.Rows(pop)("dfcity") & ", " & pendrun.Rows(pop)("dfstate")
TempNode = New ListViewItem(TempStr)
ListView2.Items.Add(TempNode)
next
How do I flush all data from tempnode?
I did find
TempNode.SubItems.clear()
but this does not appear to work
Upvotes: 0
Views: 305
Reputation: 38905
your code does not show where, when or how you try to remove the subitems. Based on the Title, I'm guessing that the LVI is already assigned to the ListView, so doing stuff to TempNode might not do anything (it could be a different LVI by this time.)
ListViewItem.SubItems.Clear()
is the correct syntax but this will also remove the Item.Label
So:
' preserve "Label" and put it back:
Dim txt As String = LV.SelectedItems(0).Text
LV.SelectedItems(0).SubItems.Clear()
LV.SelectedItems(0).Text = txt
or reference as LV.Items(IndexOfTheItemToClear).Text
another way:
' loop BACKWARDS to remove all but the last subitem (index = 0)
For n As Integer = LV.SelectedItems(0).SubItems.Count - 1 To 1 Step -1
LV.SelectedItems(0).SubItems.RemoveAt(n)
Next
since you are removing things from the collection you are looping on, you have to loop backwards otherwise your code runs out of items before N
is reached.
EDIT
Your question is not nearly as clear as you might think it is. If you are trying to remove the Item represented by TempNode in the code, you have to find it and remove it. Once TempNode has been added to the LV, you cannot recreate TempNode (TempNode = New ListViewItem(TempStr)
) because NEW makes a new ListViewItem which is not the same as the one you added earlier. So, you need to find the old one and remove it (not the same as clear[ing] the entire listview
or flush all data from tempnode
. So:
' presumably 'pendrun.Rows(pop)("RID")' is the item text, so find it:
Dim search As String = pendrun.Rows(pop)("RID")
For n as integer = 0 to LV.Items.Count - 1
if LV.Items(n).Text = search then
LV.Items.RemoveAt(n)
Exit For
End iF
Next n
It wont clear the LV but will find a specific item and remove it and the associated ListViewItem.SubItems from the LV. Matching on Text is pretty weak, so if you have something more unique like an ID, you can store that as a subitem and search that way:
if LV.Items(n).SubItems(WhereYouPutIt).Text = search then...
or even stash it in the ListViewItem.Tag
property:
if LV.Items(n).Tag = search then...
Upvotes: 1