How to add subitems to a ListView?
Asked Answered
F

3

6

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?

Ferraro answered 10/7, 2012 at 23:36 Comment(2)
@-: Changing it to details gives me the group names only (default and FIRST, e.g.).Ferraro
This is exactly why I told you I liked your questions, yesterday. Learned something myself :-)Andy
A
3

This works for me:

listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");

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++)
{
    int groupIndex = listView1.Groups.Add(new ListViewGroup(strArrGroups[i], HorizontalAlignment.Left));
    for (int j = 0; j < strArrItems.Length; j++)
    {
        ListViewItem lvi = new ListViewItem(strArrItems[j]);
        lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
        listView1.Items.Add(lvi);
        listView1.Groups[i].Items.Add(lvi);
    }
} 

It turns out that you have to add the items to the groups, and not set the group property on the item, as shown in other questions. Very, very strange.

The result is:

enter image description here

Tested in .Net 4, WinForms, VS2010

Andy answered 11/7, 2012 at 0:28 Comment(3)
@-: You rock like a hurricane!Ferraro
"int groupIndex = " is not needed.Ferraro
"groupIndex" can be passed to Groups[] instead of "i". Very helpful piece of code, thank you very much.Mariel
G
3

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!");
}
Guayaquil answered 10/7, 2012 at 23:47 Comment(2)
VERY close; in fact, this works for button1's code when I use the Details enum of the View property (as "-" suggested (not to be confused with "-" (minus))). With button 2, I'm still getting that dad-blasted "Default" group header for the first two iterations through the loop.Ferraro
I get "Default" and then "Second" groups with button2 now...why Default, and why are "First" and "Third" missing?Ferraro
A
3

This works for me:

listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");

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++)
{
    int groupIndex = listView1.Groups.Add(new ListViewGroup(strArrGroups[i], HorizontalAlignment.Left));
    for (int j = 0; j < strArrItems.Length; j++)
    {
        ListViewItem lvi = new ListViewItem(strArrItems[j]);
        lvi.SubItems.Add("Hasta la Vista, Mon Cherri!");
        listView1.Items.Add(lvi);
        listView1.Groups[i].Items.Add(lvi);
    }
} 

It turns out that you have to add the items to the groups, and not set the group property on the item, as shown in other questions. Very, very strange.

The result is:

enter image description here

Tested in .Net 4, WinForms, VS2010

Andy answered 11/7, 2012 at 0:28 Comment(3)
@-: You rock like a hurricane!Ferraro
"int groupIndex = " is not needed.Ferraro
"groupIndex" can be passed to Groups[] instead of "i". Very helpful piece of code, thank you very much.Mariel
S
2

Try adding the item after adding a subitem:

ListViewItem lvi = new ListViewItem(strArr[i]);
lvi.SubItems.Add("Ciao, Baby!");
listView1.Items.Add(lvi);    
listView1.Items[i].Group = listView1.Groups[0];

Hope this helps!

Strawberry answered 10/7, 2012 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.