Hi David,
In order to create a multi-column ListView, you'll need to create a
ColumnHeader object for each column you want to add, set the Text property
on the object to the header text for that column, and add it to the
ListView.Columns collection. Keep in mind that the columns won't be
displayed unless the View property of the ListView is set to Details.
To populate the ListView with items, create a new ListViewItem object for
each item (or row), and add the object to the ListView's Items collection.
If you have more than one column, you can create entries for each column
beyond the first one by creating ListViewSubItem objects and adding them to
the ListViewItem's SubItems collection. This may sound a little confusing,
so let me illustrate with an example:
' create the first column with a ListViewItem object
Dim firstColumn As New ListViewItem
firstColumn.Text = "First Column"
' create the next two columns with ListViewSubItem objects
' (note that the ListViewSubItem type is a sub-type of the
' ListViewItem type, so the Dim statement is a little different)
Dim secondColumn As New ListViewItem.ListViewSubItem
secondColumn.Text = "Second Column"
Dim thirdColumn As New ListViewItem.ListViewSubItem
thirdColumn.Text = "Third Column"
' add the subitems to the item
' (the order in which the subitems are added is important)
firstColumn.SubItems.Add(secondColumn)
firstColumn.SubItems.Add(thirdColumn)
' finally, add the complete item to the listview
myListView.Items.Add(firstColumn)
Also, here's some sample code to create a new ListView with a variable
number of columns and rows, and add the dynamically-created control to the
current form:
Private Sub MakeListView(ByVal rowCount As Integer, ByVal colCount As
Integer)
Dim dynamicListView As New ListView
dynamicListView.Text = String.Format("DynamicListView{0}x{1}",
rowCount, colCount)
dynamicListView.Location = New Point(16, 16)
dynamicListView.Size = New Size(180, 200)
' the view property must be set to Details for columns to display
dynamicListView.View = View.Details
' add the columns to the listview
Dim col As Integer
For col = 1 To colCount
Dim newCol As New ColumnHeader
newCol.Text = String.Format("Header{0}", col)
' add the column to the listview
dynamicListView.Columns.Add(newCol)
Next
' add rows of items and subitems to the listview
Dim row As Integer
For row = 1 To rowCount
Dim newItem As New ListViewItem
newItem.Text = String.Format("MainItem{0}", row)
' if there is more than one column, add
' subitems to the item for each additional column
Dim subrow As Integer
For subrow = 2 To colCount
' note that ListViewSubItem is a sub-type of ListViewItem
Dim newSubItem As New ListViewItem.ListViewSubItem
newSubItem.Text = String.Format("SubItem{0}.{1}", row,
subrow)
newItem.SubItems.Add(newSubItem)
Next
' add the completed row to the listview
dynamicListView.Items.Add(newItem)
Next
' add the new listview to the form
Me.Controls.Add(dynamicListView)
End Sub
Cheers,
--Joe Bork
--------------------
| From: (e-mail address removed) (David Webb)
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Subject: Dynamically creating Listview and columns
| Date: 21 Aug 2003 20:03:27 -0700
|
| Hi,
|
| Does anyone have any VBCF.NET code for dynamically creating a new
| listview and adding items and/or subitems to it? I want to create a
| listview, create X many columns, and then add data to these columns.
| I'm unsure if they are 0 or 1 based, and I keep getting errors
| whatever way I try and do it because I'm confused by the items and sub
| items.
|
| Thanks in advance for any assistance.
|
| Kind Regards,
|
| David.
|