Automatically resize a ListView control's columns to fit its data in C#

This one is easy. If you set a ListView column's width to -1, it automatically resizes to fit the data it contains. If you set the width to -2, it resizes to fit its data and column header.

This example displays buttons that let you set the ListView's column widths to 100 pixels, -1, or -2. The program uses the SetListViewColumnSizes method to set all of the column widths at once.

// Set the listView's column sizes to the same value.
private void SetListViewColumnSizes(ListView lvw, int width)
{
foreach (ColumnHeader col in lvw.Columns)
{
col.Width = width;
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.