vb.net populate multicolumn listview
Solution 1
In a multicolumn ListView
you need to set the property View
to View.Details
and then be sure to define all the column needed. So, if you haven't already done it in the Designer, you need to add the columns required to your ListView
lst_arrayShow.View = View.Details
For currentColumn As Integer = 0 To (columnsCnt - 1)
lst_arrayShow.Columns.Add("Column Nr: " & currentColumn
Next
Next, now that you have defined the columns, you could loop over your rows and create a ListViewItem
for each row.
The ListViewItem
has a Subitems
collection that correspond to the Columns defined above
For currentRow As Integer = 0 To (rowsCnt - 1)
' Create the listviewitem with the value from the first column
Dim item = new ListViewItem(yourArray(currentRow,0))
' The remainder columns after the first are added to the SubItems collection
For currentColumn As Integer = 1 To (columnsCnt - 1)
item.SubItems.Add(yourArray(currentRow,currentColumn))
Next
' Finally, the whole ListViewItem is added to the ListView
lst_arrayShow.Items.Add(item)
Next
Solution 2
This should add items in three columns in one row:
lst_arrayShow.Items.Add(New ListViewItem(New String() {"Item in column 1", "Item in column 2", "Item in column 3"}))
You could add additional items to other columns by just continuing to add strings {<string for column 1>, <string for column 2>, <... column 3>, <... column 4>, <... column 5>}
.
Andrey Baulin
Updated on November 27, 2022Comments
-
Andrey Baulin over 1 year
I'm a very newbie with VB.NET so excuse me for silly question. I have a two-dimensional array and want populate listview with it's elements. For this I'm trying use something like this:
lst_arrayShow.Items.Clear() For currentColumn As Integer = 0 To (columnsCnt - 1) lst_arrayShow.Columns.Add("") For currentRow As Integer = 0 To (rowsCnt - 1) 'lst_arrayShow.Items.Add() Next Next
What should I use instead of
'lst_arrayShow.Items.Add()
?
UPD: columnsCnt is number of columns in array and rowsCnt is number of rows in array-
Ňɏssa Pøngjǣrdenlarp over 9 yearswhat is
columnsCnt
? One of the array sizes or based on the listview? Your loop will be more complex because you need to create a ListViewItem then add SubItems to it. The LV is not a grid and does not have columns, but the SubItems are displayed as columns. -
Andrey Baulin over 9 yearsSorry. Yes, columnsCnt is number of columns in array and rowsCnt is number of rows in array
-
Ňɏssa Pøngjǣrdenlarp over 9 yearsthen just loop on RowsCount to add that many LVIs, and each if them has 1 SubItem.
-
-
Andrey Baulin over 9 yearsThanks for reply! It's almost what I need, but in listview only one row is shown.
-
Steve over 9 yearsIn which order do you have your rows and columns inside your array?. This code assumes: the first dimension of the array are rows.
-
Andrey Baulin over 9 yearsNothing wrong was with an array and your code. I just didn't set
View
property. If I addlst_arrayShow.View = View.Details
all works great. Thanks for your advices! -
Steve over 9 yearsAhh, I thought you had already done in the designer. I will add this to complete the answer.