Autosize columns for TListView
Asked Answered
T

3

16

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

Thyestes answered 3/12, 2010 at 12:15 Comment(0)
T
24

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;
Thyestes answered 3/12, 2010 at 12:24 Comment(6)
Of course, it's better to use the appropriate constants: ColumnHeaderWidth = LVSCW_AUTOSIZE_USEHEADER; or ColumnTextWidth = LVSCW_AUTOSIZE;.Parous
Just wanted to add that this doesn't work if you set the width from the Object Inspector.Altruistic
Unfortunately, this overrides the MinWidth property. It should be vice versa.Eolith
should this size be set after every time I add an item or just once?Jibheaded
I've updated the answer to use the constants.Sherard
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.Sherard
G
0

Just set columns width to -1

ListView1.Columns[0].Width:=-1;
Gormand answered 8/3, 2023 at 15:19 Comment(0)
G
-4

Try this:

// Assign vsReport;    
ListView1.ViewStyle := vsReport; 

  { // Add your items  }

// Assign vsList again;
ListView1.ViewStyle := vsList; 
Greenwell answered 18/3, 2016 at 15:43 Comment(1)
This has absolutely nothing to do with the question asked.Lipoprotein

© 2022 - 2024 — McMap. All rights reserved.