Re-sizing ListView columns in C# according to data

I was searching for an easier and simpler way to resize columns in list-view in window-forms in C#, and I got the trick on this link.

Link of the post

The following is the text copied from the above link.

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;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: