jeon jeon - 3 months ago 12
Vb.net Question

add or delete value in listview

I want to delete only one item in

ListView
, here is a screenshot:
screenshot

For i As Integer = 0 To 9 Step 1
ListView1.Items.Add("Item111" & (i + 2))
ListView1.Items(i).SubItems.Add("Sub Item 1")
'remove value
ListView1.Items(i).SubItems(1).Text = ""
'add value, error return
ListView1.Items(i).SubItems(1).Text = "200"
Next


If I delete value text
Sub item 1
it can delete, however, when add some value like
200
I get error. Why?

Answer

Looking at your code it seems pretty clear that you try to use an index to a subitems that doesn't exist. In Net the Index of any array start at index 0 not at index 1. You add just one subitem to your ListViewItem so if you want to change it you need to use the index 0 not the index 1

For i As Integer = 0 To 9 Step 1
    ListView1.Items.Add("Item111" & (i + 2))
    ListView1.Items(i).SubItems.Add("Sub Item 1")
    ' No need to set the subitem to blank and the set it to 200
    ' change the subitem directly to the new value
    ListView1.Items(i).SubItems(0).Text = "200"
Next
Comments