Results 1 to 5 of 5

Thread: Adding Item to a List view

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Resolved Adding Item to a List view

    I'm trying to add 5 columns of data to a list view.

    I've tried
    lstPortfolio.Items(Count).SubItems(1).Text = Ticker
    lstPortfolio.Items(Count).SubItems(2).Text = Convert.ToString(Shares)
    lstPortfolio.Items(Count).SubItems(3).Text = Convert.ToString(Price)
    lstPortfolio.Items(Count).SubItems(4).Text = Convert.ToString(Shares * Price)

    but I get an error telling me that Count is out of range - because there are no rows in the list view.

    I've also tried
    lstPortfolio.Items.Add(Ticker & Shares & Price & Shares * Price) but all that does is put the strings together, not in separate columns.

    I've come across a section that talks about adding an object class, but I'm HORRIBLE with getting classes to work
    Last edited by jrowe; Mar 4th, 2008 at 07:31 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Adding Item to a List view

    You can not access subitems that doesnt exist. If you're intending to add an new ListViewItem to the ListView, here's how you could do it:
    VB.NET Code:
    1. Dim Item As New ListViewItem(New String() {"this", "is", "a", "test"})
    2.         ListView1.Items.Add(Item)

    If you're intending to add subitems to an already existing listviewitem, you'd do:
    VB.NET Code:
    1. ListView1.Items(index).SubItems.Add("this is a new subitem")
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Re: Adding Item to a List view

    How would I get variables into it.

    I tried

    Dim Item As New ListViewItem(New String() {Ticker, Convert.ToString(Price * Shares), Convert.ToString(Shares), Convert.ToString(Price), Convert.ToString(Price * Shares)})

    but that just gave me 0 straight across the board.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Adding Item to a List view

    Are you sure that the variables have been assigned to prior to executing that line?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Resolved Re: Adding Item to a List view

    They are now....

    Thanks that worked perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width