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
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:
Dim Item As New ListViewItem(New String() {"this", "is", "a", "test"})
ListView1.Items.Add(Item)
If you're intending to add subitems to an already existing listviewitem, you'd do:
VB.NET Code:
ListView1.Items(index).SubItems.Add("this is a new subitem")
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.
Re: Adding Item to a List view
Are you sure that the variables have been assigned to prior to executing that line?
Re: Adding Item to a List view
They are now....:bigyello:
Thanks that worked perfectly