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.

Share:
24,414
Admin
Author by

Admin

Updated on August 06, 2020

Comments

  • Admin
    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
    Devin Burke about 13 years
    But 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
    Admin about 13 years
    The 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
    Admin about 13 years
    LOL you're very welcome @Hans. It's not the "wait" that jerks me, it's the fact that the timer lied to me. :)
  • Admin
    Admin about 13 years
    Yeah, @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
    crdx almost 12 years
    This 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
    bkribbs over 8 years
    Wow, running both works, great trick!
  • 4LegsDrivenCat
    4LegsDrivenCat over 5 years
    Very 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?