Home All Groups Group Topic Archive Search About

How to add new rows to Listview

Author
2 Mar 2009 4:27 AM
hon123456
Dear all,

          I know that I can add text ListView like that Set Item =
ListView1.ListItems.Add(, , "abc")
Then I want to add another new row to the ListView, how can I do that?
I have tried
Set Item = ListView1.ListItems.Add(2, , "abc") But the "2" is only for
column index but not row
index. Please suggest me how to Add a new row in Listview.

Thanks

Author
2 Mar 2009 11:07 AM
Irfan S. Fazli
Use listview..AddItem("ABC")
defaults to add at end of list.
--
IRFAN.



Show quoteHide quote
"hon123456" wrote:

> Dear all,
>
>           I know that I can add text ListView like that Set Item =
> ListView1.ListItems.Add(, , "abc")
> Then I want to add another new row to the ListView, how can I do that?
> I have tried
> Set Item = ListView1.ListItems.Add(2, , "abc") But the "2" is only for
> column index but not row
> index. Please suggest me how to Add a new row in Listview.
>
> Thanks
>
Author
2 Mar 2009 1:11 PM
Jan Hyde
hon123456 <peterhon***@yahoo.com.hk>'s wild thoughts were
released on Sun, 1 Mar 2009 20:27:18 -0800 (PST) bearing the
following fruit:

>Dear all,
>
>          I know that I can add text ListView like that Set Item =
>ListView1.ListItems.Add(, , "abc")
>Then I want to add another new row to the ListView, how can I do that?
>I have tried
>Set Item = ListView1.ListItems.Add(2, , "abc") But the "2" is only for
>column index but not row
>index. Please suggest me how to Add a new row in Listview.

the "2" is the row index not the column index. You code
should work as is so I'm not sure what the problem is here.


--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Author
3 Mar 2009 4:09 AM
hon123456
thanks Jan,

               But you can try to use the abouve code. You will find
that it only add
a new column but not a new row.

Thanks
Author
3 Mar 2009 12:47 PM
Jan Hyde
hon123456 <peterhon***@yahoo.com.hk>'s wild thoughts were
released on Mon, 2 Mar 2009 20:09:04 -0800 (PST) bearing the
following fruit:

>thanks Jan,
>
>               But you can try to use the abouve code. You will find
>that it only add
>a new column but not a new row.

I did try your code. I suspect you don't have it in report
mode.

J

>Thanks

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Author
3 Mar 2009 4:27 AM
Bob Butler
"hon123456" <peterhon***@yahoo.com.hk> wrote in message
news:22d5eeee-c8ed-4fa3-88a3-cd7176f630ba@v5g2000prm.googlegroups.com...
> Dear all,
>
>          I know that I can add text ListView like that Set Item =
> ListView1.ListItems.Add(, , "abc")
> Then I want to add another new row to the ListView, how can I do that?
> I have tried
> Set Item = ListView1.ListItems.Add(2, , "abc") But the "2" is only for
> column index but not row
> index. Please suggest me how to Add a new row in Listview.

The Index is the row index and specifes where to position the row being
added.  Usually you just omit that and it adds the new row at the end.
Running the code you posted results in two list items being added; if you
are seeing it as two "columns" then my guess is that you didn't set the View
property right and you're getting the Icon view which does look like 2
columns when you don't specify icons.

Dim oItem As MSComctlLib.ListItem
Dim oHdr As MSComctlLib.ColumnHeader
Dim x As Long
ListView1.View = lvwReport
ListView1.FullRowSelect = True
Set oHdr = ListView1.ColumnHeaders.Add(, , "Item Name")
Set oHdr = ListView1.ColumnHeaders.Add(, , "Sub Item")
Set oHdr = ListView1.ColumnHeaders.Add(, , "Second Sub")
For x = 1 To 10
  Set oItem = ListView1.ListItems.Add(, , "Item " & CStr(x))
  oItem.SubItems(1) = "Sub 1-" & CStr(x)
  oItem.SubItems(2) = "Sub 2-" & CStr(x)
Next
Set oItem = ListView1.ListItems.Add(4, , "This one goes in the middle")

If you don't have sub items for each row then you can also try using the
List view:

Dim oItem As MSComctlLib.ListItem
Dim x As Long
ListView1.View = lvwList
Set oItem = ListView1.ListItems.Add(, , "Item 1")
Set oItem = ListView1.ListItems.Add(, , "Item 2")
Author
6 Mar 2009 6:13 AM
hon123456
Thanks to You all. I know how to use listview now.