C#: How to add subitems in ListView

150,472

Solution 1

Like this:

ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("SubItem");
listView1.Items.Add(lvi);

Solution 2

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!

Solution 3

I think the quickest/neatest way to do this:

For each class have string[] obj.ToListViewItem() method and then do this:

foreach(var item in personList)
{
    listView1.Items.Add(new ListViewItem(item.ToListViewItem()));
}

Here is an example definition

public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }
    public DateTime DOB { get; set; }
    public uint ID { get; set; }

    public string[] ToListViewItem()
    {
        return new string[] {
            ID.ToString("000000"),
            Name,
            Address,
            DOB.ToShortDateString()
        };
    }
}

As an added bonus you can have a static method that returns ColumnHeader[] list for setting up the listview columns with

listView1.Columns.AddRange(Person.ListViewHeaders());

Solution 4

I've refined this using an extension method on the ListViewItemsCollection. In my opinion it makes the calling code more concise and also promotes more general reuse.

internal static class ListViewItemCollectionExtender
{
    internal static void AddWithTextAndSubItems(
                                   this ListView.ListViewItemCollection col, 
                                   string text, params string[] subItems)
    {
        var item = new ListViewItem(text);
        foreach (var subItem in subItems)
        {
            item.SubItems.Add(subItem);
        }
        col.Add(item);
    }
}

Calling the AddWithTextAndSubItems looks like this:

// can have many sub items as it's string array
myListViewControl.Items.AddWithTextAndSubItems("Text", "Sub Item 1", "Sub Item 2"); 

Hope this helps!

Solution 5

add:

.SubItems.Add("asdasdasd");

to the last line of your code so it will look like this in the end.

listView1.Items.Add("sdasdasdasd").SubItems.Add("asdasdasd");
Share:
150,472
P. Duw
Author by

P. Duw

Updated on August 29, 2020

Comments

  • P. Duw
    P. Duw almost 4 years

    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("sdasdasdasd");
    //How to add "asdasdasd" under value?