Add items programatically to ListView

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Using VS2005

I have added a Listview containing 4 columns in design mode...

How can i populate it programatically ?

I tried... but only "111" shows up in the list view. Also, I want it to
create a new line when adding it.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim str(4) As String


Dim itm As ListViewItem


str(0) = "111"
str(1) = "894"
str(2) = "888"
str(3) = "222"

itm = New ListViewItem(str)
ListView1.Items.Add(itm)

End Sub
 
Rob said:
Using VS2005

I have added a Listview containing 4 columns in design mode...

How can i populate it programatically ?

I tried... but only "111" shows up in the list view. Also, I want it to
create a new line when adding it.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim str(4) As String


Dim itm As ListViewItem


str(0) = "111"
str(1) = "894"
str(2) = "888"
str(3) = "222"

itm = New ListViewItem(str)
ListView1.Items.Add(itm)

End Sub

You do have to walk str(0) through str(3) with some kind of For Loop I
would think, if that str() is some kind of an array.

for loop

itm = New ListViewItem(str(i))
ListView1.Items.Add(itm)

next
 
I think what the OP meant was this:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim str(4) As String


Dim itm As ListViewItem


str(0) = "111"
str(1) = "894"
str(2) = "888"
str(3) = "222"

itm = New ListViewItem(str(0))
ListView1.Items.Add(itm)
ListView1.SubItems.Add(str(1))
ListView1.SubItems.Add(str(2))
ListView1.SubItems.Add(str(3))

End Sub
 
Rob,

Make sure the list view is in Details mode.
ListView1.View = System.Windows.Forms.View.Details

Hope this helps,


Steve
 
Johnny,

I think I am missing something because "SubItems" code does not work...

Thanks,
Rob
 
Oh I think I see the problem...

ListView1.SubItems.Add(str(1))
ListView1.SubItems.Add(str(2))
ListView1.SubItems.Add(str(3))

should be...

itm.SubItems.Add(str(1))
itm.SubItems.Add(str(2))
itm.SubItems.Add(str(3))

Thanks
 
True

Sorry - my mistake. I copied your code and forgot to change that...

Cheers,
Johnny J.
 
Back
Top