C# ListView images not showing at runtime

2.1k views Asked by At

Hello I've made a simple ListView which has 2 columins, it's a Detail ListView, i've created an ImageList and added my images inside, I set the column ImageIndex(I've tried with ImageKey also) for both of my columns and Images show inside designer but once I run the program there is no images being shown.

Here is a screenshot of designer http://i.imgur.com/ZohRUon.png

2

There are 2 answers

0
Idle_Mind On

Despite showing the icons in the column header at design-time, apparently the ListView control in .Net doesn't actually support this feature:

This behavior occurs because the .NET implementation of the System.Windows.Forms.ColumnHeader class does not include the capability to assign an image to the ColumnHeader control. You can assign only text to a ColumnHeader control.

The code in PRB: Cannot Assign Images to a ColumnHeader Control in a Windows Forms ListView Control in Visual C# .NET worked fine for me, however.

After adding the code and running my test app, my column headers had the same icon in them at run-time that was assigned to them at design-time.

2
Hans Passant On

This is a flaw in the designer, it doesn't properly serialize the ColumnHeader.ImageIndex property. You'll for example see that the image is gone again when you close the designer window and re-open it.

The KB article that @Idle linked is much too heavy-handed, you simply need to do the same thing you did in the designer. Setting the ImageIndex again. Do so in the form constructor. I can't tell what the correct code might be from the screen-shot but it ought to resemble:

    public Form1() {
        InitializeComponent();
        listView1.Columns[0].ImageIndex = 0;
        listView1.Columns[1].ImageIndex = 1;
    }