Add ListView items

  • Thread starter Thread starter Matteo
  • Start date Start date
M

Matteo

I am kinda new to VB.NET so please bear with me. I am
working on a project that accesses a database and returns
an arrayList of strings. I am then supposed to parse the
string into separate strings that hold pieces of
information. Now I am supposed to take those pieces of
information and place them in a mulitcolumn listview. I
have my listview set up and I have the loop set up to
cycle through the arrayList and parse each line, however,
for the life of me I cannot figure out how to add data to
each column. Here is an example to better explain what I
am trying to do:

I have a ListView set up with the following columns
(Name, Subject, Rate, Degree and Hours). SO I have 5
columns. Now I parse each line from the arrayList into 5
strings (strName, strSub, strRate, strDeg and strHours).
Now how do I loop through and tell the program to add
strName under the Name column, strSub under the Subject
column, etc. And when that is done with the first line,
go on to the next line in the listview and repeat the
process until the listview is empty. Any help would be
GREATLY appreciated.

Matteo
 
Hi Matteo,

The main thing with controls as the listview and the treeview is that it are
trees (a dataset is that also).

You hangs everytime an object in that tree with an add.

Think about it as a tree.

You "add" created items (nodes) to that tree.

To that created items you add "subitems" (subnodes)

When you are used to that it becomes suddenly easy to use those controls.

But when you have problems send snippets of code (not a whole program, to
that will seldom be looked at in newsgroups).

(And paste those code first in your notebook and back in the mail, that
makes reading easier and the faster you get an answer)

I hope this helps?

Cor
 
Matteo

Assuming you already have a loop set up to get the items out of your string array then you need to do the following inside the loop..

With ListView1.Items.Add(strName
.SubItems.Add(strSub
.SubItems.Add(strRate
.SubItems.Add(strDeg
.SubItems.Add(strHours
End Wit

Hope this helps

Gary
 
Matteo said:
I am kinda new to VB.NET so please bear with me. I am

Try this:

'First set up the columns
With mListViewItemsList
.FullRowSelect = True
.MultiSelect = False
.GridLines = True
.View = View.Details
.Columns.Add("ID", 40, HorizontalAlignment.Center)
.Columns.Add("Public", 50, HorizontalAlignment.Left)
.Columns.Add("Question", (210, HorizontalAlignment.Left)
.HideSelection = False
End With

'Then fill in the data
Dim iIndex As Integer
Dim iRowID As Integer
Dim objRow As DataRow

mLabelRecordCount.Text = objDataSet.Tables(0).Rows.Count.ToString
For iIndex = 0 To objDataSet.Tables(0).Rows.Count - 1

objRow = objDataSet.Tables(0).Rows.Item(iIndex)
iRowID = Int32.Parse(objRow.Item("ID"))

objListViewItem = New ListViewItem(iRowID.ToString())
objListViewItem.SubItems.Add(objRow.Item("IsPublic").ToString())
objListViewItem.SubItems.Add(objRow.Item("Question").ToString())

Next
 
Back
Top