Auto widths in ListView
24,414
Solution 1
Review the ColumnHeader.AutoResize() method. Call it after populating the Items, the form's OnLoad() method is the first chance.
Solution 2
Use both resizing options after populating the list:
myListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
myListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
This will size columns to the width of column data, and then restore minimum width for column headers without completely trashing the original auto-sizing.
Author by
Admin
Updated on August 06, 2020Comments
-
Admin over 3 years
How can we set the width of each column according to the length of its content in a ListView? I hate having to keep changing the size of each column at runtime. Is there an MSDN doc that has this info? I can't find where I should be looking
Thank you
-
Devin Burke about 13 yearsBut what if the column is blank? One of the common problems with column auto-resizing is that, although ideally it would size according to the content with a minimum width of the column header, the auto-size properties force an empty column to be about two pixels in width.
-
Admin about 13 yearsThe Timer on S/O is really giving me the s s. I had one minute left to wait, and after that minute, it said 46seconds left, so I waited 46seconds. Then it said 10seconds left. :@
-
Admin about 13 yearsLOL you're very welcome @Hans. It's not the "wait" that jerks me, it's the fact that the timer lied to me. :)
-
Admin about 13 yearsYeah, @Justin, I agree with you. I just realised that. What I did was resize according to content, and when I know that the contents of the columns is not likely to be wider than the column headers themselves, I just resize according to the header. But still, that's not the most desirable way of doing it.
-
crdx almost 12 yearsThis is the problem I'm having. I want something like "resize by header, then by content ONLY if content is wider than the header".
-
bkribbs over 8 yearsWow, running both works, great trick!
-
4LegsDrivenCat over 5 yearsVery nice trick. But why does it work? According to MSDN, ColumnHeaderAutoResizeStyle.HeaderSize should shrink column to the header width but this does not happen? Why? Bug in listview that luckily is very useful or bad documentation?