C#: How to add subitems in ListView

后端 未结 10 1252
眼角桃花
眼角桃花 2020-11-29 07:33

Creating an item(Under the key) is easy,but how to add subitems(Value)?

listView1.Columns.Add(\"Key\");
listView1.Columns.Add(\"Value\");
listView1.Items.Add         


        
10条回答
  •  旧巷少年郎
    2020-11-29 07:59

    Suppose you have a List Collection containing many items to show in a ListView, take the following example that iterates through the List Collection:

    foreach (Inspection inspection in anInspector.getInspections())
      {
        ListViewItem item = new ListViewItem();
        item.Text=anInspector.getInspectorName().ToString();
        item.SubItems.Add(inspection.getInspectionDate().ToShortDateString());
        item.SubItems.Add(inspection.getHouse().getAddress().ToString());
        item.SubItems.Add(inspection.getHouse().getValue().ToString("C"));
        listView1.Items.Add(item);
      }
    

    That code produces the following output in the ListView (of course depending how many items you have in the List Collection):

    Basically the first column is a listviewitem containing many subitems (other columns). It may seem strange but listview is very flexible, you could even build a windows-like file explorer with it!

提交回复
热议问题