Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to create a list view with 3 columns, but I need also 2 more columns with related data that should not be viewed. Is there a way to do this?
Thank you.

I use Microsoft Visual Studio 2022 and this application is in Windows Forms.

What I have tried:

I thought it would be possible to manipulate a listview the same way we do with datagridview, but it's not possible.
Posted
Updated 15-Apr-24 3:11am
v2
Comments
Graeme_Grant 14-Apr-24 9:01am    
What framework? Winform? Wpf? MAUI? etc...
Ismael_1999 15-Apr-24 9:12am    
Visual Studio with Windows Forms
Richard MacCutchan 14-Apr-24 10:37am    
Maybe you need to rethink exactly what you are trying to achieve.
Ismael_1999 15-Apr-24 9:13am    
I need more data related to the lines of the List View. Do you have any other suggestion?
Richard MacCutchan 15-Apr-24 9:25am    
"Do you have any other suggestion?"
About what? It is far from clear what you are trying to do.

There's no standard way to do this in Windows Forms.

  • The alternative I use is to manage a ListView's configuration in another object and initialize and populate the ListView appropriately.  See this screenshot as an example: https://ravib.com/takestock/2/takestock2.png[^].

    My configuration object also supports sorting and various column auto-size modes, and can be serialized as a user preference.

  • You could also try the poor man's solution of setting the "hidden" column width's to zero, but you would have to manually disable attempts to resize the column.
/ravi
 
Share this answer
 
Comments
Pete O'Hanlon 15-Apr-24 5:19am    
My 5 for your more elegant solution.
Ravi Bhavnani 15-Apr-24 21:53pm    
Thanks, Pete!

/ravi
Ismael_1999 15-Apr-24 9:18am    
Hi, Ravi.
I saw your screenshot but didn't understand your solution. Could you explain better?
Thanks.
Dave Kreskowiak 15-Apr-24 12:28pm    
In WinForms, you cannot hide a column or "make it invisible". The only true way of "hiding" columns is if the column isn't there to begin with. Just don't create the column.
As an alternative to Ravi's answer, there is a quick and dirty "hack" that you can use, assuming you are using Windows Forms here. When you add an item into a listview, you are adding an instance of a ListViewItem. This class has a Tag property that you can use as a property bag to store pretty much anything in. It's entirely possible for you to store what you need in this property.
 
Share this answer
 
Comments
Ismael_1999 15-Apr-24 9:21am    
Hi, Pete. Your suggestion is interesting, but as I have to store more than 1 information, I need to think of how I can join and split this information. Thanks.
Pete O'Hanlon 15-Apr-24 9:27am    
You can store a class in there, so you could put whatever information you need in that.
Ismael_1999 15-Apr-24 15:06pm    
Pete. Your suggestion was perfect. Thanks a lot.
Pete O'Hanlon 15-Apr-24 16:35pm    
You’re welcome
If you are using data binding, like in this article here: Data binding a ListView[^], then the ListView control becomes a view of the data, not the data container itself. Then you only need to show the properties as columns, as required. This means there is no requirement to "hide" any columns.
 
Share this answer
 
Comments
Ismael_1999 16-Apr-24 16:15pm    
Hi, Graeme_Grant. I studied your solution and think it's a good project. I'll try it.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900