I have in C# Net 2.0 Windows Forms a ListView with about ten columns filled with data at startup of the application. The data is huge, so it cannot be reloaded fast in-between. My ListView has the Details View on and allows the user the resizing of each column separately.
The user shall be able to hide any of the ten columns or multiple of them at once and unhide the columns again any time in non-specific row. The data shall be not deleted while hiding the column.
Following things I have tried but the result were not satisfying:
1) Resizing the column with size 0 will make it somewhat disappearing until the user starts to play with the columns. The user accidently will resize them back because in my ListView there is the option allowed for the user to resize each column manually.
2) With just deleting the column out occurs the following problem: When I try to add the column back, the column doesn't remember its position and size. The position is the main problem, I'll try to explain why. If my user decides to first hide "column 2" and then "column 3" and the user later unhides 3 before 2, then "Index 2" doesn't exist, so I cannot insert at Index No. 3 and an exception rises. Even if I remember the Index position before deleting I cannot simply put the Column just back to that index, because I don't know if previous columns where already hided too or which column is missing before and after or hided or not. So the scenario could be like that: 1 shown, 2 hided, 3 hided too, 4 shown, 5 hided, 6 hided, 7 hided , 8 shown, 9 hided, 10 hided.
Possible solutions "1)" and "2)" are automatically out ruled in the scenario. Better is even making the width of the column to zero, but since my user shall be able to resize columns any time by need, with resizing to zero it cannot be hided from the user. The user will unhide it by resize and my system will think it's still hidden etc. etc. and it looks not professional if hidden columns can be just "resized back" or how we could name that else.
Has someone a better idea? Why the listView Column has no "visible" or "hide" property, I wonder? If someone did that already before please post a solution.
I have to add that I use autoresizing of all columns in my listview when data is being added. For this reason the answer below doesn't work. The resize event can't detect the width of -1. "All invisible columns" with width 0 will be resized back, when data was added. Since the listview cuts out data which overlaps column length, I need to autoresize it permanently. Explorer has not this problem because it fits the columns to the length of the data. C# has not such advanced listview, here I have to set the columns to -1 each time when data being added. In this conception doesn't work the idea of column.width = 0 for hiding the columns.
OK, Your problem is in fact How to hide a ListView Column?. This has been asked by many many people on the web. I've tried searching many however couldn't find anything. I've ended up with the only solution: Set the column width to Zero. This will work with some trick here:
It works almost perfectly. However when user moves mouse over the
pipe
at the position of the hidden column, there is aCursor
indicator appearing to notify user something like There is a Zero-width column here, just hold mouse down and drag to resize it. Of course, user can't resize it from Zero because weCancel
it and makeNewWidth = 0
(as the code above does). But theCursor
notifying such operation makes it a little nasty, here is the screen shot demonstrating the problem:To solve this problem is not easy. At least that's what I feel. I thought of this solution which seems to work OK. The idea is we have to detect if the mouse is over near the pipe of a hidden column, we have to set
Cursor = Cursors.Arrow
. Here is the whole class which I think works great for you: