locked
Adding SubItems to ListView?

    Question

  • Hi all I am having some trouble in adding SubItems to my ListView. I am able to add to a single column by doing ListViewItem.add("Item 1") but I have 3 columns and unsure of how to add values to these columns.

    I am doing something like this at the moment:

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")
    Lstv_SubItem.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)
    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)


    However, doing this I get random errors. Is there a more cleaner version of this?

    Error: Cannot add or insert the item 'OFKL4122006 838096R' in more than one place. You must first remove it from its current location or clone it.
    Parameter name: item

    Additionally, is it possible to refresh the ListView? Because I am updating the ListView from a click method my ListView is being added to but I want it to be overwritten? For instance: my ListView contains

    "Item1, Item2"

    When I click on another element I want Item1 and Item2 to be erased and contain the new data. Although I am using a
    Lstv_OfficeInfo.Clear() but it always remains Cleared, i.e. no items are found in it.
    Monday, March 03, 2008 4:20 PM

Answers

  • The ListViewItem.SubItems.Add routine has 3 overloads. Two of which allow Text to be passed instead of a ListViewSubItem

     

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.listviewitem.listviewsubitemcollection.add(VS.71).aspx

     

    Try this.

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

     

    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_OfficeInfo.SubItems.Add("CompanyName")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

     

    ListView1.Items.Add(Lstv_OfficeInfo)

     

    Monday, March 03, 2008 4:31 PM
  •  OnamC wrote:
    Hi all I am having some trouble in adding SubItems to my ListView. I am able to add to a single column by doing ListViewItem.add("Item 1") but I have 3 columns and unsure of how to add values to these columns.

    I am doing something like this at the moment:

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")
    Lstv_SubItem.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)
    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)


    However, doing this I get random errors. Is there a more cleaner version of this?

    Error: Cannot add or insert the item 'OFKL4122006 838096R' in more than one place. You must first remove it from its current location or clone it.
    Parameter name: item

    Additionally, is it possible to refresh the ListView? Because I am updating the ListView from a click method my ListView is being added to but I want it to be overwritten? For instance: my ListView contains

    "Item1, Item2"

    When I click on another element I want Item1 and Item2 to be erased and contain the new data. Although I am using a
    Lstv_OfficeInfo.Clear() but it always remains Cleared, i.e. no items are found in it.

     

    It sounds like you are re-using the same Lstv_SubItem, like:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")
    Lstv_SubItem.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)

    Lstv_SubItem.Text = "Changed the text here"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)

    Lstv_SubItem.Text = "Changed the text again"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    As the error says, you cannot re-use the same item. You have to do it like:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem1 As New ListViewItem.ListViewSubItem

    Dim Lstv_SubItem2 As New ListViewItem.ListViewSubItem

    Dim Lstv_SubItem3 As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_SubItem1.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem2.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem2)

     

    Lstv_SubItem3.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem3)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    Or alternatively:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem1 As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_SubItem1.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem1 = New ListViewItem.ListViewSubItem

    Lstv_SubItem1.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem1 = New ListViewItem.ListViewSubItem

    Lstv_SubItem1.Text = "New Item2"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    My previous post outlines a simpler method, too.

     

    As far as clearing it goes, it should work fine providing you use Clear() before re-adding all the items.

    Monday, March 03, 2008 4:42 PM

All replies

  •  

    What are the random errors that you're getting?
    Monday, March 03, 2008 4:23 PM
  • The ListViewItem.SubItems.Add routine has 3 overloads. Two of which allow Text to be passed instead of a ListViewSubItem

     

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.listviewitem.listviewsubitemcollection.add(VS.71).aspx

     

    Try this.

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

     

    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_OfficeInfo.SubItems.Add("CompanyName")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

    Lstv_OfficeInfo.SubItems.Add("Another Column")

     

    ListView1.Items.Add(Lstv_OfficeInfo)

     

    Monday, March 03, 2008 4:31 PM
  •  OnamC wrote:
    Hi all I am having some trouble in adding SubItems to my ListView. I am able to add to a single column by doing ListViewItem.add("Item 1") but I have 3 columns and unsure of how to add values to these columns.

    I am doing something like this at the moment:

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")
    Lstv_SubItem.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)
    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)


    However, doing this I get random errors. Is there a more cleaner version of this?

    Error: Cannot add or insert the item 'OFKL4122006 838096R' in more than one place. You must first remove it from its current location or clone it.
    Parameter name: item

    Additionally, is it possible to refresh the ListView? Because I am updating the ListView from a click method my ListView is being added to but I want it to be overwritten? For instance: my ListView contains

    "Item1, Item2"

    When I click on another element I want Item1 and Item2 to be erased and contain the new data. Although I am using a
    Lstv_OfficeInfo.Clear() but it always remains Cleared, i.e. no items are found in it.

     

    It sounds like you are re-using the same Lstv_SubItem, like:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")
    Lstv_SubItem.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)

    Lstv_SubItem.Text = "Changed the text here"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)

    Lstv_SubItem.Text = "Changed the text again"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    As the error says, you cannot re-use the same item. You have to do it like:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem1 As New ListViewItem.ListViewSubItem

    Dim Lstv_SubItem2 As New ListViewItem.ListViewSubItem

    Dim Lstv_SubItem3 As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_SubItem1.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem2.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem2)

     

    Lstv_SubItem3.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem3)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    Or alternatively:

     

    Code Snippet

    Dim Lstv_OfficeInfo As New ListViewItem

    Dim Lstv_SubItem1 As New ListViewItem.ListViewSubItem


    Lstv_OfficeInfo.Text = Offices_DR("ID")

    Lstv_SubItem1.Text = Offices_DR("CompanyName")
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem1 = New ListViewItem.ListViewSubItem

    Lstv_SubItem1.Text = "New Item"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)

     

    Lstv_SubItem1 = New ListViewItem.ListViewSubItem

    Lstv_SubItem1.Text = "New Item2"
    Lstv_OfficeInfo.SubItems.Add(Lstv_SubItem1)


    Me.Lstv_OfficeInfo.Items.Add(Lstv_OfficeInfo)

     

     

     

    My previous post outlines a simpler method, too.

     

    As far as clearing it goes, it should work fine providing you use Clear() before re-adding all the items.

    Monday, March 03, 2008 4:42 PM
  • This is what I am getting.

    Error: Cannot add or insert the item 'OFKL4122006 838096R' in more than one place. You must first remove it from its current location or clone it.
    Parameter name: item

    I tried the code Daniel and it seems to work fine Smile however the Clear method doesn't seem to work. I place it at the beginning of the function and still it remains blank? Not sure of why this is happening?

    Monday, March 03, 2008 4:57 PM
  • Can you post the exact code you are using?

    Monday, March 03, 2008 5:21 PM
  • Sorry everyone I realised that I was using:

    Code Snippet

    ListView1.Clear()


    I am supposed to be using:

    Code Snippet

    ListView1.Items.Clear()



    Thanks all for the help, much appreciated Smile
    Tuesday, March 04, 2008 9:42 AM