27

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?
Prix
  • 19,417
  • 15
  • 73
  • 132
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248

10 Answers10

30

You whack the subitems into an array and add the array as a list item.

The order in which you add values to the array dictates the column they appear under so think of your sub item headings as [0],[1],[2] etc.

Here's a code sample:

//In this example an array of three items is added to a three column listview
string[] saLvwItem = new string[3];

foreach (string wholeitem in listofitems)
{
     saLvwItem[0] = "Status Message";
     saLvwItem[1] = wholeitem;
     saLvwItem[2] = DateTime.Now.ToString("dddd dd/MM/yyyy - HH:mm:ss");

     ListViewItem lvi = new ListViewItem(saLvwItem);

     lvwMyListView.Items.Add(lvi);
}
  • Er... I've used this technique about four times in the last fortnight. Why the downvotes? –  Apr 08 '09 at 08:58
  • +1 for offering a perfectly valid alternative to the other answers. Why you received a downvote I do not know. – Dan Tao May 22 '09 at 12:52
  • 4
    An easier alternative is to do it as such: `listView.Items.Add(new ListViewItem(new string[]{"Col1", "SubItem2", "SubItem3", "And so on"}));` – c00000fd Oct 01 '13 at 00:52
25

Like this:

ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("SubItem");
listView1.Items.Add(lvi);
Marcus L
  • 4,030
  • 6
  • 34
  • 42
15

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!

Prix
  • 19,417
  • 15
  • 73
  • 132
jasonco
  • 1,517
  • 1
  • 15
  • 22
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());
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
3

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!

BrianB
  • 424
  • 2
  • 13
  • Very nice, but what is passed as the ListView.ListViewItemCollection when calling this extension method? – B. Clay Shannon-B. Crow Raven Jul 06 '12 at 20:46
  • @ClayShannon: as it's an extension method it "extends" the behaviour of the .NET FX ListView.ListViewItemCollection. The compiler turns the extension method style into a call to the static method. So `myListViewControl.Items.AddWithTextAndSubItems("Text", "Sub Item 1", "Sub Item 2");` becomes `ListViewItemCollectionExtender.AddWithTextAndSubItems(myListViewControl.Items, "Text", "Sub Item 1", "Sub Item 2");` Compiler magic!! – BrianB Jul 10 '12 at 09:53
2

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");
alpharedx
  • 21
  • 1
2

Generally:

ListViewItem item = new ListViewItem("Column1Text")
   { Tag = optionalRefToSourceObject };

item.SubItems.Add("Column2Text");
item.SubItems.Add("Column3Text");
myListView.Items.Add(item);
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
2
ListViewItem item = new ListViewItem();
item.Text = "fdfdfd";
item.SubItems.Add ("melp");
listView.Items.Add(item);
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
2

Create a listview item

ListViewItem item1 = new ListViewItem("sdasdasdasd", 0)
item1.SubItems.Add("asdasdasd")
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

Great !! It has helped me a lot. I used to do the same using VB6 but now it is completely different. we should add this

listView1.View = System.Windows.Forms.View.Details;
listView1.GridLines = true; 
listView1.FullRowSelect = true;
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Patrick
  • 21
  • 1