Autosize columns for TListView

11,727

I got the answer. Setting the column width to LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER solved the problem.

Use LVSCW_AUTOSIZE setting to set the column header to the size of the largest subitem text in the column,

and a LVSCW_AUTOSIZE_USEHEADER setting to set the column header to the size of the text in the column header.

uses CommCtrl;

ListView1.Columns[0].Width := LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER;
Share:
11,727
Bharat
Author by

Bharat

Delphi developer since 2008

Updated on June 06, 2022

Comments

  • Bharat
    Bharat almost 2 years

    I want to auto-size all the columns in the TListView. I am using below code, but its not doing any thing.

    ListView1.Columns.Add.Caption := 'Field Name';
    ListView1.Columns.Items[0].Autosize := True;
    

    How can i auto-size the columns of TListView in Delphi.

    I set my ViewStyle to vsReport.

    Thanks in advance

  • Uli Gerhardt
    Uli Gerhardt over 13 years
    Of course, it's better to use the appropriate constants: ColumnHeaderWidth = LVSCW_AUTOSIZE_USEHEADER; or ColumnTextWidth = LVSCW_AUTOSIZE;.
  • Aaron
    Aaron over 10 years
    Just wanted to add that this doesn't work if you set the width from the Object Inspector.
  • Ken White
    Ken White about 8 years
    This has absolutely nothing to do with the question asked.
  • user1580348
    user1580348 almost 6 years
    Unfortunately, this overrides the MinWidth property. It should be vice versa.
  • Armin Taghavizad
    Armin Taghavizad over 5 years
    should this size be set after every time I add an item or just once?
  • dan-gph
    dan-gph over 2 years
    I've updated the answer to use the constants.
  • dan-gph
    dan-gph over 2 years
    I think you just need LVSCW_AUTOSIZE_USEHEADER. That will auto-size the column to fit the largest of the header or the items. Or-ing the two constants doesn't appear to work.