This old MS KB article works for me : PRB: Cannot Assign Images to a ColumnHeader Control in a Windows Forms ListView Control in Visual C# .NET
(tested on Windows 10 21H1, VS 2022)
Winforms ListView ColumnHeader Image
There has been an acknowledged bug in the Winforms ListView ColumnHeader in that it will show an icon/image in design view but not when the program is running.
It's causing me an issue in a program I am writing and the bug is still there in VS2019 sadly.
Is anybody aware of a workround please? Even ObjectListView doesn't seem to show images in headers.
-
Castorix31 81,461 Reputation points
2022-06-27T12:33:05.6+00:00
1 additional answer
Sort by: Most helpful
-
Reza Aghaei 4,936 Reputation points MVP
2022-07-11T23:51:58.327+00:00 .NET Version 3.1, 5.0, 6.0 - Fixed
The bug has been fixed starting from .NET 3.1, so it works as expected in all later .NET versions (like 5.0 and 6.0), but it has not been fixed in .NET Framework (like .NET Framework 4.8).
.NET Framework 4.x - Designer bug - Just assign ImageIndex in code
The bug in .NET Framework versions (I tested 4.x versions) is just because of the VS Designer failing to serialize the ImageIndex property. It basically means you just need to manually set the image indices yourself in the code. For example, assuming that you have stored image indices in Tag, then you can use such code to show images:
public Form1() { InitializeComponent(); foreach (ColumnHeader item in listView1.Columns) { int imageIndex = -1; if (int.TryParse(item.Tag.ToString(), out imageIndex)) item.ImageIndex = imageIndex; } }
If upgrading from .NET Framework to .NET is an option, you can upgrade to .NET 6.0 to fix the problem, otherwise, just use the above workaround. The old workaround which is mentioned in the other answer, is no longer necessary.