How to add data to multiple column ListView?

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
If the ListView has multiple columns, how do I add data to columns that are not in order? In the other word, how to choose a column in ListView to add data? For example, there are 4 columns and I want to add data to Column1 and Column3.

Thanks.
DanYeung
 
This is the code I normally use:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str(3) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem[/SIZE]
 
[SIZE=2]str(0) = [/SIZE][SIZE=2][COLOR=#0000ff]"Value"[/COLOR][/SIZE]
[SIZE=2]str(1) = [COLOR=#0000ff]"Value"[/COLOR][/SIZE]
[SIZE=2]str(2) = [COLOR=#0000ff]"Value"[/COLOR][/SIZE]
[SIZE=2]str(3) = [/SIZE][SIZE=2][COLOR=#0000ff]"Value"[/COLOR][/SIZE]
 
[SIZE=2]itm = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem(str) [/SIZE]
 
[SIZE=2]MyListView.Items.Add(itm)[/SIZE]

Just pass an empty string for columns you don't need values in.
 
you could also do it this way


VB.NET:
dim item as new listviewitem
with item
.subitems(0).text="this"
.subitems(2).text="that"
end with
'adds to the listview (whatever it is)
currentlistview.items.add(item)

hope it helps mate

any questions, let me know

regards

adam
 
Last edited by a moderator:
Thanks, Adam. The code is in a click event. It worked the first time the button was clicked, but bump the second time the button was clicked. Here is my code:
VB.NET:
[COLOR=#0000ff]Dim listViewItem As New ListViewItem[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("0")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("1")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("2")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("3")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("4")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("5")[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems.Add("6")[/COLOR]
 
[COLOR=#0000ff]Dim transaction As Transaction[/COLOR]
[COLOR=#0000ff]For Each transaction In objTransList.Transactions[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(0).Text = transaction.TransactionDate.ToShortDateString[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(1).Text = ""[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(2).Text = transaction.Description.ToString[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(3).Text = "D"[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(4).Text = ""[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(5).Text = transaction.Amount.ToString[/COLOR]
[COLOR=#0000ff]listViewItem.SubItems(6).Text = CalcBalance.ToString[/COLOR]
[COLOR=#0000ff]ListView1.Items.Add(listViewItem)[/COLOR]
[COLOR=#0000ff]Next[/COLOR]
The error was "Cannot add or insert the item '10/1/2006' in more than one place. You must first remove it from its current location or clone it.
Parameter name: item" when executing ListView1.Items.Add(listViewItem). What did I miss? Thanks.

DanYeung
 
Last edited by a moderator:
You forgot to reinitalise the item object - I have inserted the code below: You need to initalise a new object for every line.

VB.NET:
[COLOR=black]Dim listViewItem As New ListViewItem[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("0")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("1")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("2")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("3")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("4")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("5")[/COLOR]
[COLOR=black]listViewItem.SubItems.Add("6")[/COLOR]
 
[COLOR=black]Dim transaction As Transaction[/COLOR]
[COLOR=black]For Each transaction In objTransList.Transactions[/COLOR]
 
[COLOR=green]'this was missing[/COLOR]
[COLOR=red]listViewItem = New ListViewItem[/COLOR]
[COLOR=green][COLOR=red]listViewItem.SubItems.Add("0")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("1")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("2")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("3")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("4")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("5")[/COLOR]
[COLOR=red]listViewItem.SubItems.Add("6")[/COLOR]
[/COLOR][COLOR=green]'^^^^^^^^^^^^[/COLOR]
 
 
[COLOR=black]listViewItem.SubItems(0).Text = transaction.TransactionDate.ToShortDateString[/COLOR]
[COLOR=black]listViewItem.SubItems(1).Text = ""[/COLOR]
[COLOR=black]listViewItem.SubItems(2).Text = transaction.Description.ToString[/COLOR]
[COLOR=black]listViewItem.SubItems(3).Text = "D"[/COLOR]
[COLOR=black]listViewItem.SubItems(4).Text = ""[/COLOR]
[COLOR=black]listViewItem.SubItems(5).Text = transaction.Amount.ToString[/COLOR]
[COLOR=black]listViewItem.SubItems(6).Text = CalcBalance.ToString[/COLOR]
[COLOR=black]ListView1.Items.Add(listViewItem)[/COLOR]
[COLOR=black]Next[/COLOR]
 
Which is pretty ugly - my way is much neater:

VB.NET:
[COLOR=#0000ff]Dim[/COLOR][SIZE=2] str(6) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem[/SIZE]

 
[COLOR=black]Dim transaction As Transaction[/COLOR]
[COLOR=black]For Each transaction In objTransList.Transactions[/COLOR]
 
[COLOR=black]str(0) =  transaction.TransactionDate.ToShortDateString[/COLOR]
[COLOR=black]str(1) =  ""[/COLOR]
[COLOR=black]str(2) =  transaction.Description.ToString[/COLOR]
[COLOR=black]str(3) =  "D"[/COLOR]
[COLOR=black]str(4) = ""[/COLOR]
[COLOR=black]str(5) = transaction.Amount.ToString[/COLOR]
[COLOR=black]str(6) = CalcBalance.ToString[/COLOR]
[COLOR=black][/COLOR] 
itm = [SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem(str) [/SIZE]
[SIZE=2]ListView1.Items.Add(itm)[/SIZE]
[COLOR=black][/COLOR] 
[COLOR=black]Next[/COLOR]
 
Back
Top