如何将子项添加到ListView?

时间:2012-07-10 23:36:54

标签: c# winforms listview listviewitem

我正在尝试使用子项目工作的Listview的最简单的可能示例。但是这段代码:

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];
    }
}

...不显示子项(“Ciao,Baby!”)。它显示:

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

...但我希望它是:

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

更奇怪(在我看来),我明白了:

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

FIRST
-----
uno dos twa quad

...使用此代码:

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];
        }
    }
}

更新

如果你问我,ListView的View属性会有点奇闻趣事:

默认的“ LargeIcon ”设置最初显示(并且SmallIcon具有相同的效果)。 “详细信息”仅为我提供了群组标题(没有项目) “列表”为我提供了项目(一行一行,我想要),但没有组/标题 “ Tile ”给了我:

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

...使用button1的代码并且:

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

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

...使用button2的代码

或者:

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?

~~~ 侧面(或底部)问题:由于我住在StackOverflow上,是否有可能成为双重公民,并且这样做是否有任何税收优惠?

3 个答案:

答案 0 :(得分:3)

您需要为SubItem添加另一列。您subitem未显示的原因是因为没有列。试试这个。

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

答案 1 :(得分:3)

这对我有用:

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

事实证明,您必须将项目添加到组中,而不是在项目上设置组属性,如其他问题所示。非常非常奇怪。

结果是:

enter image description here

在.Net 4,WinForms,VS2010中测试

答案 2 :(得分:2)

添加子项后尝试添加项目:

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

希望这有帮助!