Add items programatically to ListView

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
 
M

Mr. Arnold

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
 
J

Johnny Jörgensen

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
 
P

PlatinumBay

Rob,

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

Hope this helps,


Steve
 
R

Rob

Johnny,

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

Thanks,
Rob
 
R

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
 
J

Johnny Jörgensen

True

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

Cheers,
Johnny J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top