如何:使用 Windows 窗体 ListView 控件在列中显示子项

Windows 窗体 ListView 控件可以在“详细信息”视图中显示每个项的附加文本或子项。 第一列显示项文本,例如员工编号。 第二列、第三列及后续各列显示第一、第二及后续关联的子项。

向列表项添加子项

  1. 添加所需的任何列。 由于第一列将显示项的 Text 属性,因此需要一个比子项多的列。 有关添加列的更多信息,请参阅 如何将列添加到 Windows 窗体 ListView 控件

  2. 调用由项的 Add 属性返回的集合的 SubItems 方法。 下面的代码示例设置列表项的员工名称和部门。

    // Adds two subitems to the first list item.
    listView1.Items[0].SubItems.Add("John Smith");
    listView1.Items[0].SubItems.Add("Accounting");
    
    
    ' Adds two subitems to the first list item
    ListView1.Items(0).SubItems.Add("John Smith")
    ListView1.Items(0).SubItems.Add("Accounting")
    
    

另请参阅