简体   繁体   中英

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

Late to the party, but I've just spent some time trying to solve a similar problem. The answer I found was that when clearing the list before filling, you must use

listviewGroupItems.Items.Clear();

not

listviewGroupItems.Clear();

The ListView.Clear() method clears everything from the control--including the columns

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.


在此处输入图片说明


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

I had the same issue today. I had coded listView.Clear() as user2867342 mentioned. I needed to change that to listView.Items.Clear() , but that did not make the columns appear. The columns were there, and I could click on them and resize them, but they were completely blank.

I had a ListView set to Details mode. I also had set the OwnerDraw property to true, because I wanted to paint my own progress bar column. MSDN says the following about the OwnerDraw property (emphasis mine):

A ListView control is normally drawn by the operating system. In order to customize the appearance of ListView items, subitems, and column headers, set the OwnerDraw property to true and provide a handler for one or more of the following events: DrawItem, DrawSubItem, DrawColumnHeader. This is called owner drawing. When the View property is set to View.Details, all three events occur; otherwise, only the DrawItem event occurs.

I had to implement the DrawColumnHeader event. In my case, the defualt worked fine, so the method sets the DrawDefault event parameter to true. After implementing this event handler, the column headers appeared correctly:

...Windows.Forms designer code...
listView.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this.listView_DrawColumnHeader);
...

private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

One more thing to check I just found: OwnerDraw must be false.

Copy & Paste from previous project and forgot that I used OwnerDraw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM