How to add subitems to a ListView?

B. Clay Shannon picture B. Clay Shannon · Jul 11, 2012 · Viewed 12.6k times · Source

I'm trying to get the simplest possible example of a Listview with subitems working. But this code:

private void button1_Click(object sender, EventArgs e) {
    listView1.Groups.Add(new ListViewGroup("Kannst du mich sehen?", HorizontalAlignment.Left));
    string[] strArr = new string[4] { "uno", "dos", "twa", "quad" };
    for (int i = 0; i < strArr.Length; i++)
    {
        ListViewItem lvi = new ListViewItem(strArr[i]);
        listView1.Items.Add(lvi);
        lvi.SubItems.Add("Ciao, Baby!");
        listView1.Items[i].Group = listView1.Groups[0];
    }
}

...does not display the subitems (the "Ciao, Baby!"s). It shows:

Kannst du mich sehen?
---------------------
uno   dos   twa   quad

...but I want it to be:

Kannst du mich sehen?
---------------------
uno Ciao, Baby!
dos Ciao, Baby!
twa Ciao, Baby!
quad    Ciao, Baby!

Even stranger (it seems to me), I get this:

Default
-------
uno dos twa quad    uno dos twa quad

FIRST
-----
uno dos twa quad

...with this code:

private void button2_Click(object sender, EventArgs e) {
    string[] strArrGroups = new string[3] { "FIRST", "SECOND", "THIRD" };
    string[] strArrItems = new string[4] { "uno", "dos", "twa", "quad" };
    for (int i = 0; i < strArrGroups.Length; i++)
    {
        listView1.Groups.Add(new ListViewGroup(strArrGroups[i], HorizontalAlignment.Left));
        for (int j = 0; j < strArrItems.Length; j++) {
            ListViewItem lvi = new ListViewItem(strArrItems[j]);
            listView1.Items.Add(lvi);
            lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
            listView1.Items[j].Group = listView1.Groups[i];
        }
    }
}

UPDATE

The ListView's View property acts a little gonzo, if you ask me:

The default "LargeIcon" setting acts as originally shown (and SmallIcon has the same effect). "Detail" gives me the Group header only (no items) "List" gives me the items (one on a line, as I want), but no group/header "Tile" gives me:

Kannst du mich sehen?
---------------------
uno     dos
twa     quad

...with button1's code and:

Default
---------------------
uno     dos
twa     quad
uno     dos
twa     quad

FIRST
---------------------
uno     dos
twa     quad

...with button2's code

Either:

1) I'm stupid
2) ListView is very counterintuitive
-or
3) Nobody ever wants to use the ListView the way I'm trying to use it...if that's the case, should I be using a different control instead?

~~~ Side (or bottom) question: Since I live on StackOverflow, is it possible to become a dual citizen, and are there any tax benefits in doing so?

Answer

John Woo picture John Woo · Jul 11, 2012

You need to add another column for the SubItem. The reason why your subitem is not shown is because there is no column for it. Try this one.

listView1.Columns.Add("Column 1"); // you can change the column name
listView1.Columns.Add("Column 2");
string[] strArr = new string[4] { "uno", "dos", "twa", "quad" };
foreach (string x in strArr)
{
    ListViewItem lvi = listView1.Items.Add(x);
    lvi.SubItems.Add("Ciao, Baby!");
}