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.