Search code examples
c#.netwinformslistviewlistviewitem

Why does the ListView refuse to display its columns, items, and subitems (shows only Group)?


The ListView seems to be as cantankerous as a polecat and as temperamental as a [elided by the P.C. police]

With help from - and others, I was able to get a ListView working just as I wanted it to (http://stackoverflow.com/questions/11423537/how-to-add-subitems-to-a-listview).

Now I'm converting that simple demo for use in a real app.

I create the columns in the form's Load event:

listViewGroupsItems.Columns.Add(colHeadName);
listViewGroupsItems.Columns.Add(colHeadRate);

...but they don't display. However, there is a blank line above my first item. So why are no text values being displayed. Are they "there" but invisible? Why would they get wiped out?

Anyway, what I want to see is:

column1Title    column2Title
Group1Name
Item        subitem
Item        subitem
Group2Name
Item        subitem
Group1Name
Item        subitem
Item        subitem
Item        subitem
Item        subitem

...but what I actually see is just:

[blank line]
Group1Name

...that's it!

The ListView's View property is set to Details; otherwise, all of the properties are the default values.

My listview code is:

private void AddGroupsAndItems() {
    Dictionary<string, string> GroupsDict = PlatypusData.GetGroupsForTopLevel(Convert.ToInt32(labelTopLevel.Tag));
    int currentGroup = 0;
    foreach (KeyValuePair<string, string> entry in GroupsDict) {
        string GroupNumber = entry.Key;
        string GroupName = entry.Value;
        listViewGroupsItems.Groups.Add(new ListViewGroup(GroupName, HorizontalAlignment.Left));

        Dictionary<string, string> ItemsDict = PlatypusData.GetItemsForGroup(GroupNumber);
        foreach (KeyValuePair<string, string> itemEntry in ItemsDict) {
            string itemID = itemEntry.Key;
            string itemName = itemEntry.Value;
            ListViewItem lvi = new ListViewItem(string.Format("{0} ({1})", itemName, itemID));
            lvi.SubItems.Add(PlatypusData.GetDuckbillNameForItemId(itemID));
            listViewGroupsItems.Items.Add(lvi);
            listViewGroupsItems.Groups[currentGroup].Items.Add(lvi);
        }
        currentGroup++;
    }
}

UPDATE

I changed my code in the form's Load event from:

var colHeadName = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_Billing_Account_Client, Width = 160 };
var colHeadRate = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_RatePlan_ID, Width = 120 };
listViewCustomerBillAccountsClients.Columns.Add(colheadName); //colHeadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate); //colHeadRate);

...to:

ColumnHeader colheadName = new ColumnHeader();
ColumnHeader colheadRate = new ColumnHeader();
listViewCustomerBillAccountsClients.Columns.Add(colheadName); 
listViewCustomerBillAccountsClients.Columns.Add(colheadRate);

...and it made no difference at all.

It would seem that the ColumnHeader constructor should be able to take a string of what it should display, but even when I do that:

ColumnHeader colheadName = new ColumnHeader("This");
ColumnHeader colheadRate = new ColumnHeader("That");

...there is still no change (according to Intellisense or whatever it's called, the string arg is an ImageKey, but I thought I'd try just out of thoroughness/frustration.


Solution

  • You're missing column headers in your code. (fixed)

    Per the MSDN:

    "If your ListView control does not have any column headers specified and you set the View property to View.Details, the ListView control will not display any items. If your ListView control does not have any column headers specified and you set the View property to View.Tile, the ListView control will not display any subitems."

    Granted, you'll probably need to make more adjustments than what you see in my SS, but it at least answers your question as to why you are getting blanks.


    enter image description here


    Edit (Changed lines, although changing them back to your code didn't skew my successful results):

    lvi.Group = listViewGroupsItems.Groups[currentGroup];
    listViewGroupsItems.Items.Add(lvi);