【问题标题】:vb.net populate multicolumn listviewvb.net 填充多列列表视图
【发布时间】:2014-12-26 17:42:47
【问题描述】:

我是 VB.NET 的新手,所以请原谅我的愚蠢问题。 我有一个二维数组,想用它的元素填充列表视图。为此,我正在尝试使用这样的东西:

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 是数组的行数

【问题讨论】:

  • 什么是columnsCnt?数组大小之一还是基于列表视图?您的循环会更复杂,因为您需要创建一个 ListViewItem 然后将 SubItems 添加到它。 LV 不是网格,也没有列,但是 SubItems 显示为列。
  • 对不起。是的,columnsCnt 是数组中的列数,rowsCnt 是数组中的行数
  • 然后在 RowsCount 上循环添加那么多 LVI,如果每个 LVI 有 1 个子项。

标签: vb.net listview multiple-columns


【解决方案1】:

在多列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

【讨论】:

  • 感谢您的回复!这几乎是我需要的,但在列表视图中只显示了一行。
  • 数组中的行和列的顺序是什么?这段代码假设:数组的第一个维度是行。
  • 数组和您的代码没有问题。我只是没有设置View 属性。如果我添加 lst_arrayShow.View = View.Details 一切都很好。感谢您的建议!
  • 啊,我以为你已经在设计器中完成了。我将添加此以完成答案。
【解决方案2】:

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

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>} 来将其他项目添加到其他列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2015-08-19
    • 2014-01-05
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多