重置链接到listview的listviewitem

时间:2013-12-29 22:29:14

标签: vb.net listview

我有一个listview绑定到listviewitem,如下所示 - 问题是我需要在计时器上清除数据库并从头开始,

我知道要清除我刚才的列表视图

listview.items.clear()

问题是我还需要清除listviewitem,否则它会将相同的信息重新绑定到listview。

宣称:

    Dim TempStr(7) As String
    Dim TempNode As ListViewItem

有问题的陈述:

            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

如何从tempnode刷新所有数据?

我找到了

TempNode.SubItems.clear()

但这似乎不起作用

1 个答案:

答案 0 :(得分:1)

您的代码未显示您尝试删除子项的位置,时间或方式。根据标题,我猜测LVI已经分配给了ListView,所以对TempNode做的事情可能什么都不做(此时它可能是一个不同的LVI。)

ListViewItem.SubItems.Clear()是正确的语法,但这也会删除Item.Label所以:

 ' preserve "Label" and put it back:
 Dim txt As String = LV.SelectedItems(0).Text
 LV.SelectedItems(0).SubItems.Clear()
 LV.SelectedItems(0).Text = txt

或引用为LV.Items(IndexOfTheItemToClear).Text

另一种方式:

    ' 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

因为你要从正在循环的集合中删除东西,所以你必须向后循环,否则你的代码会在达到N之前耗尽项目。

修改

你的问题并不像你想象的那么清楚。如果您尝试在代码中删除由TempNode表示的项目,则必须找到它并将其删除。将TempNode添加到LV后,您无法重新创建TempNode(TempNode = New ListViewItem(TempStr)),因为NEW会生成一个 new ListViewItem,它与您之前添加的ListViewItem不同。因此,您需要找到旧的并删除它(与clear[ing] the entire listviewflush all data from tempnode不同。所以:

' 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

它不会清除LV,但会找到一个特定的项目并从LV中删除它和相关的ListViewItem.SubItems。在Text上匹配非常弱,所以如果你有一些像ID一样独特的东西,你可以将它存储为子项并以这种方式搜索:

 if LV.Items(n).SubItems(WhereYouPutIt).Text = search then...

甚至将其隐藏在ListViewItem.Tag属性中:

if LV.Items(n).Tag = search then...