0

I have a ListView with a view of View.Details and I want to AutoSize the columns. I am aware of the ColumnHeader's AutoResize method and currently I'm just iterating through them like so:

var lMode = ( listView.Items.Count == 0 ) ? ColumnHeaderAutoResizeStyle.HeaderSize : ColumnHeaderAutoResizeStyle.ColumnContent;
foreach ( ColumnHeader lColumnHeader in listView.Columns )
{
    lColumnHeader.AutoResize(lMode);
}

The problem with this technique is that if I have a column which happens to have no data in it, the width gets set to a point the header is no longer available. Is there a nice way of conditionally setting the ColumnHeaderAutoResizeStyle based on whether or not there are contents in the column?

If there is something in the column I want to use ColumnHeaderAutoResizeStyle.ColumnContent, but if there is nothing in the column, I want to use ColumnHeaderAutoResizeStyle.HeaderSize.

2 Answers 2

2

This is a method I've been using for a little while now that does not involve measuring strings.

The bool parameter is because I call the method by itself (and set the parameter to true), and sometimes I call it directly after an Items.AddRange(ListViewItem[]) call, in which I'd wrap both calls in the Begin/End Update calls.

    /// <summary>
    /// Set the column widths to the widest of the items
    /// or the column header text.
    /// </summary>
    /// <param name="useUpdate">Whether to use Begin/End Update methods
    /// to pause the drawing of the list view.</param>
    private void SetColumnWidths(bool useUpdate)
    {
        if(useUpdate)
            list.BeginUpdate();

        int width;
        int totalWidth = 0;

        foreach (ColumnHeader col in list.Columns)
        {
            // The last column's width will be the leftover
            //   width of the list view.
            if (list.Columns.Count != col.DisplayIndex)
            {
                col.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize); 
                width = col.Width;

                col.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                if (width > col.Width)
                    col.Width = width;

                totalWidth += col.Width;
            }
            else
            {
                col.Width = (list.ClientSize.Width - totalWidth);
            }

        }

        if(useUpdate)
            list.EndUpdate();

    }
0
0

Unless a better answer is found, let me at least give an answer to my question in case other people want this functionality. The best way I could think of was to manually scan the table and measure the strings myself. I created an extension method for this purpose:

public static void AutoResizeAllColumns( this ListView listView )
{
    if ( listView.Columns.Count == 0 ) return;

    var lGraphics = Graphics.FromHwnd( listView.Handle );

    foreach ( ColumnHeader lColumnHeader in listView.Columns )
    {
        var lColumnHeaderTextSize = lGraphics.MeasureString( lColumnHeader.Text, listView.Font );

        var lColumnIndex = lColumnHeader.Index;
        var lAnyContents = listView.Items
            .Cast<ListViewItem>()
            .Select( x => lGraphics.MeasureString( x.SubItems[ lColumnIndex ].Text, listView.Font ) )
            .Any( x => x.Width > lColumnHeaderTextSize.Width );

        lColumnHeader.AutoResize( lAnyContents ? ColumnHeaderAutoResizeStyle.ColumnContent : ColumnHeaderAutoResizeStyle.HeaderSize );
    }
}

I have not measured this method for speed or anything, but it seems to work. Just hoping to find a more elegant solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.