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

Bharat
  • 6,828
  • 5
  • 35
  • 56

3 Answers3

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;
dan-gph
  • 16,301
  • 12
  • 61
  • 79
Bharat
  • 6,828
  • 5
  • 35
  • 56
0

Just set columns width to -1

ListView1.Columns[0].Width:=-1;
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
-4

Try this:

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

  { // Add your items  }

// Assign vsList again;
ListView1.ViewStyle := vsList; 
Undo
  • 25,519
  • 37
  • 106
  • 129
Churry
  • 15