Multiple Columns in a ListView??

S

simchajoy2000

Hi,

I am trying to use a VB.NET listview object to display information
from a datatable. I need to have two columns of information but I
don't want the user to be able to select each column individually -
that's why I am attempting to use a listview rather than a datagrid.

I have been reading a lot of postings in this newsgroup concerning
multiple columns in listviews and I have tried to follow all the
advice I have found - but I am still having trouble. I have not been
able to get the second column of information to show up in the
listview.

Here is my code:

Dim tbl As DataTable = New DataTable
Dim col As Integer
Dim row As DataRow
Dim lvi As ListViewItem

tbl.Columns.Add("test")
tbl.Columns.Add("test2")

row = tbl.NewRow
row(0) = "help"
row(1) = "help me"
tbl.Rows.Add(row)

NodeList.BeginUpdate()
For col = 1 To tbl.Columns.Count
'Create a listview column for each dataset column
NodeList.Columns.Add(New ColumnHeader)
Next

row = Nothing
For Each row In tbl.Rows
'Create a listview item for each row in dataset, using text from
col0
lvi = New ListViewItem(CStr(row(0)))
col = 1
Do While col < tbl.Columns.Count - 1
'Append the subsequent columns
lvi.SubItems.Add(row(col))
col += 1
Loop
NodeList.Items.Add(lvi)
Next

Does anyone know what I am doing wrong??

Also, for some reason, I am having trouble with my Visual Studio 2003
program - it doesn't detect/recognize my code or any errors I make
until I compile it - this just started happening yesterday when I
started working with this problem - so I don't know if they are in any
way connected but I thought I would just mention it just in case.

Any advice would be greatly appreciated!!

Joy
 
I

Imran Koradia

Do While col < tbl.Columns.Count - 1
'Append the subsequent columns

I think you're actually missing the last column with the line of code
above..

change that line to either:
Do While col <= tbl.Columns.Count - 1

or

Do While col < tbl.Columns.Count


hope this helps..
Imran.
 
J

Joy Labadie

Sorry - that was a typo - you are right - but unfortunately that doesn't
resolve my problem - any other ideas??
 
G

Greg Burns

Option Strict On


Dim tbl As DataTable = New DataTable

Dim row As DataRow
tbl.Columns.Add("test")
tbl.Columns.Add("test2")

row = tbl.NewRow
row(0) = "help"
row(1) = "help me"
tbl.Rows.Add(row)

For col As Integer = 1 To tbl.Columns.Count
'Create a listview column for each dataset column
NodeList.Columns.Add(New ColumnHeader)
Next

NodeList.BeginUpdate() <-- moved this here, (don't think it will help with
add columns)
For Each row In tbl.Rows
'Create a listview item for each row in dataset, using text from col0()
Dim lvi As ListViewItem
lvi = New ListViewItem(row(0).ToString)
Dim col As Integer = 1
Do While col <= tbl.Columns.Count - 1
'Append the subsequent columns
lvi.SubItems.Add(row(col).ToString)
col += 1
Loop
NodeList.Items.Add(lvi)
Next
NodeList.EndUpdate() ' <-- this might be what your missing

HTH,
Greg
 

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