Auto widths in ListView [duplicate]

2019-04-28 22:40发布

问题:

This question already has an answer here:

  • C# ListView Column Width Auto 10 answers

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

回答1:

Review the ColumnHeader.AutoResize() method. Call it after populating the Items, the form's OnLoad() method is the first chance.



回答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.



回答3:

I got it:

viewer.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);