开发者

Listview items (and subitems)

Hope you guys can help me out once more before I go nuts. Iv'e been searching ligh and low, here and on google. Gone through MSDN back and forth, but to no avail... I just don't understand what I'm doing wrong...

I have a form, where I have added a listview (without any changes to the default settings when it's beeing dragged to the form).

After a day of fooling around, I got to simply trying an easy example since nothing I wanted to do would work. The example got to serve as some kind of "If this doesn't work, then it's time to ask for help!"...

This is the example;

ListViewItem item = new ListViewItem();
item.Text = "FirstItem";
item.SubItems.Add("A subitem");
item.SubItems.Add("A second subitem");
item.SubItems.Add("A third subitem");
listView1.Items.Add(item);

All that this example gives me, it "FirstItem", nothing else...

My own version that I wanted to try, is the following:

I have a dictionary named "members". it contains two two strings, "name" as Key, and "LastSeen" ad Value.

I'm trying the following:

if (GroupMembers.Count > 0)
{
    listView1.Items.Clear();
    key2name getname = new key2name();
    ListViewItem lvi = new ListViewItem("Name");
    ListViewItem lvi2 = new ListViewItem("Last seen");
    listView1.Columns.Add("Name", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("LastSeen", -2, HorizontalAlignment.Left);
    foreach (KeyValuePair<string, string> member in GroupMembers)
    {
        lvi.SubItems.Add(member.Key);
        lvi2.SubItems.Add(member.Value);
    }
    listView1.Items.AddRange(new ListViewItem[] { lvi, lvi2 });
}

What I am trying to acomplish, is that my listview will show two columns, with the first column name with the text "Name", and the second "Last seen". Under each of those columns, I want the content of my dictionary to show up.

Can someone please help me shed some light over this before I go completely nuts over this? Thanks...

Although the answers were right, I still don't get the utput I want, and I'm not sure of how to do it..

Now I do see the columns, but I get this:

|  Name    | LastSeen   |
|Last seen | 2011-09-06 |
|Name      | Full Name  |

What I want is:

|  Name    | LastSeen   |
|FullName  | 2011-09-06 |
|FullName  |开发者_开发技巧 2011-09-06 |
|FullName  | 2011-09-06 |

The dictionary curently contains 3 KeyValuePairs


The example is right, you should put the listView in details mode and add the columns.

Your code after that is wrong you should not create two list items lvi and lv2, just create only 1 inside the foreach, its text is the text of first column, add subitems to it and add the item to the listview inside the foreach.

You will see the columns only when you have set the ListView in details mode.


Make sure your ListView has it's View Property set to Details in order for columns to show.

You may also want to set some of your listview's properties at design time, including your columns, unless they are dynamic.

    if (GroupMembers.Count > 0)
    {
        listView1.Items.Clear();
        key2name getname = new key2name();

        //If your going to do this here, you will want to clear your Columns
        //listView1.Columns.Clear();
        //However, I would suggest you put this elsewhere, or better yet set at design time.

        listView1.Columns.Add("Name", -2, HorizontalAlignment.Left);
        listView1.Columns.Add("LastSeen", -2, HorizontalAlignment.Left);


        foreach (KeyValuePair<string, string> member in GroupMembers)
        {
            //This creates a new 'row' in your listview, and populates the first column
            // with the Key
            ListViewItem lvi = new ListViewItem(member.Key);

            //This populates the second column with the value
            lvi.SubItems.Add(member.Value);


            listView1.Items.Add(lvi)
        }
    }


If all that you are seeing is large icons, you need to set the View property on the ListView to Details. Otherwise the code looks more or less correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜