如何子项目添加到ListView? [英] How to add subitems to a ListView?

查看:115
本文介绍了如何子项目添加到ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让与子项目的工作ListView的最简单可行的例子。但是,这code:

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

...不显示子项(即Ciao的,宝贝!S)。它显示:

...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

...这个code:

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

更新

ListView控件的视图属性的行为有点奇闻趣事,如果你问我:

UPDATE

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

默认的 LargeIcon 的最初显示(和SmallIcon具有相同效果)设置的行为。
详细信息的给我的组头只(没有项目)
列表的给我的项目(​​一个上线,因为我想),但没有组/头
瓷砖的给我:

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

...按钮1用的code和:

...with button1's code and:

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

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

...用按钮2的code

...with button2's code

或者:

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?

推荐答案

这对我的作品:

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.

的结果是:

在测试.NET 4中,的WinForms,VS2010

Tested in .Net 4, WinForms, VS2010

这篇关于如何子项目添加到ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆