By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Multiple Columns in a ListView??

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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.
Nov 21 '05 #2

P: n/a

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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a
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

"Joy Labadie" <si***********@yahoo.com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...

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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.