C# ListView Column Width Auto

187,303

Solution 1

You gave the answer: -2 will autosize the column to the length of the text in the column header, -1 will autosize to the longest item in the column. All according to MSDN. Note though that in the case of -1, you will need to set the column width after adding the item(s). So if you add a new item, you will also need to assign the width property of the column (or columns) that you want to autosize according to data in ListView control.

Solution 2

Use this:

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

from here

Solution 3

I made a program that cleared and refilled my listview multiple times. For some reason whenever I added columns with width = -2 I encountered a problem with the first column being way too long. What I did to fix this was create this method.

private void ResizeListViewColumns(ListView lv)
{
    foreach(ColumnHeader column in lv.Columns)
    {
        column.Width = -2;
    }
}

The great thing about this method is that you can pretty much put this anywhere to resize all your columns. Just pass in your ListView.

Solution 4

If you have ListView in any Parent panel (ListView dock fill), you can use simply method...

private void ListViewHeaderWidth() {
        int HeaderWidth = (listViewInfo.Parent.Width - 2) / listViewInfo.Columns.Count;
        foreach (ColumnHeader header in listViewInfo.Columns)
        {
            header.Width = HeaderWidth;
        }
    }

Solution 5

There is another useful method called AutoResizeColumn which allows you to auto size a specific column with the required parameter.

You can call it like this:

listview1.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);
listview1.AutoResizeColumn(2, ColumnHeaderAutoResizeStyle.ColumnContent);
listview1.AutoResizeColumn(3, ColumnHeaderAutoResizeStyle.HeaderSize);
listview1.AutoResizeColumn(4, ColumnHeaderAutoResizeStyle.HeaderSize);
Share:
187,303
Kai
Author by

Kai

we all have our segfaults.

Updated on July 08, 2022

Comments

  • Kai
    Kai almost 2 years

    How can I set the column width of a c# winforms listview control to auto. Something like width = -1 / -2 ?

  • Qsiris
    Qsiris over 11 years
    this will set the width to the width of the last item in your ListView
  • toddmo
    toddmo over 9 years
    But what if you want the maximum of the header or the data width? With no flicker?
  • prototype0815
    prototype0815 over 6 years
    I use your code but if I have only one column in my listview, each time I get a horizontal scrollbar. If there are at least two columns the code works well. Do you know where does this behaviour come from?
  • Artem Zh.
    Artem Zh. over 5 years
    @toddmo Use both strings. And, if neccesary, between listView.BeginUpdate(); ... listView.EndUpdate();
  • Eric G
    Eric G almost 5 years
    Note that while the MSDN article references ColumnHeader, that does just mean the column. Code example: myListView.Columns[0].Width = -1;
  • ToolmakerSteve
    ToolmakerSteve almost 3 years
    Can't use both simultaneously. If attempt to "or" together ColumnHeaderAutoResizeStyle.ColumnContent and ColumnHeaderAutoResizeStyle.HeaderSize (resulting in a value of "3"), at runtime that line gives exception "System.ComponentModel.InvalidEnumArgumentException: 'The value of argument 'headerAutoResize' (3) is invalid for Enum type 'ColumnHeaderAutoResizeStyle'. Parameter name: headerAutoResize' ". Alternatively, if you execute both those lines, the second one will overwrite the first value, as if the first line was not there.
  • ToolmakerSteve
    ToolmakerSteve almost 3 years
    Use Math.Max: colFirstName.Width = Math.Max(colFirstName.Width, 60);
  • Ocean Airdrop
    Ocean Airdrop almost 2 years
    This worked a treat!