Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add subitems to a ListView?

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?

like image 466
B. Clay Shannon-B. Crow Raven Avatar asked Jul 10 '12 23:07

B. Clay Shannon-B. Crow Raven


Video Answer


1 Answers

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

like image 110
dash Avatar answered Sep 18 '22 19:09

dash