vb.net填充多列listview

时间:2014-12-25 00:31:11

标签: vb.net listview multiple-columns

我是VB.NET的新手,请原谅我这个愚蠢的问题。 我有一个二维数组,并希望用它的元素填充listview。为此,我尝试使用这样的东西:

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

我应该使用什么而不是'lst_arrayShow.Items.Add()
UPD: columnsCnt是数组中的列数,rowsCnt是数组中的行数

2 个答案:

答案 0 :(得分:3)

这应该在一行中的三列中添加项目:

lst_arrayShow.Items.Add(New ListViewItem(New String() {"Item in column 1", "Item in column 2", "Item in column 3"}))

您只需继续添加字符串{<string for column 1>, <string for column 2>, <... column 3>, <... column 4>, <... column 5>}即可向其他列添加其他项目。

答案 1 :(得分:3)

在多列ListView中,您需要将属性View设置为View.Details,然后确保定义所需的所有列。因此,如果您尚未在Designer中完成此操作,则需要添加ListView所需的列

lst_arrayShow.View = View.Details
For currentColumn As Integer = 0 To (columnsCnt - 1)
    lst_arrayShow.Columns.Add("Column Nr: " & currentColumn
Next

接下来,既然您已经定义了列,就可以遍历行并为每行创建ListViewItem
ListViewItem有一个Subitems集合,对应于上面定义的列

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