How to add subitems to a ListView?

一个人想着一个人 提交于 2019-12-19 03:12:09

问题


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?


回答1:


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:

Tested in .Net 4, WinForms, VS2010




回答2:


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!");
}



回答3:


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!



来源:https://stackoverflow.com/questions/11423537/how-to-add-subitems-to-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!