Listview angst

G

Guest

My form has a listview control, set to show detail view. It has 2 columns:
workbooks and worksheets

I run the following code, trying to get each Excel workbook name as a main
item and each worksheet as a new subordinate item. Some open workbooks have
1 worksheet, but one has 11 worksheets. so, I'm expecting to see

Workbook1
worksheet1
worksheet2
Workbook2
worksheet1
worksheet2
worksheet3

and so forth

No matter what I do the listivew only shows a single line for each workbook
with that workbook's first worksheet on that same line. It's probably
something simple, but the the heck am I missing??


For Each wb In xl.Workbooks
lvi = New ListViewItem(wb.Name)
For Each ws In wb.Worksheets
lvi.SubItems.Add(ws.Name)
Next
Me.WorkbooksListview.Items.Add(lvi)
Next
 
G

Guest

Never mind - I figured out that I have to add groups. For anybody who cares,
the code I ended up using is

Dim lvi As ListViewItem = Nothing
Dim lvg As ListViewGroup = Nothing
Dim lv As ListView = Me.WorkbooksListview
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

For Each wb In xl.Workbooks
lvg = New ListViewGroup(wb.Name)
lv.Groups.Add(lvg)
For Each ws In wb.Worksheets
lvi = New ListViewItem("", lvg)
lvi.SubItems.Add(ws.Name)
lv.Items.Add(lvi)
Next

Next
 

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