Export (0) Print
Expand All

ListView.AutoResizeColumn Method

Resizes the width of the given column as indicated by the resize style.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void AutoResizeColumn (
	int columnIndex,
	ColumnHeaderAutoResizeStyle headerAutoResize
)
public void AutoResizeColumn (
	int columnIndex, 
	ColumnHeaderAutoResizeStyle headerAutoResize
)
public function AutoResizeColumn (
	columnIndex : int, 
	headerAutoResize : ColumnHeaderAutoResizeStyle
)
Not applicable.

Parameters

columnIndex

The zero-based index of the column to resize.

headerAutoResize

One of the ColumnHeaderAutoResizeStyle values.

Exception typeCondition

ArgumentOutOfRangeException

columnIndex is greater than 0 when Columns is a null reference (Nothing in Visual Basic)

-or-

columnIndex is less than 0 or greater than the number of columns set.

InvalidEnumArgumentException

headerAutoResize is not a member of the ColumnHeaderAutoResizeStyle enumeration.

Calling this method is only effective once the ListView and containing Form have been constructed, and the ListView column headers and columns are populated with items. If new items are added to the ListView, the columns will not resize unless AutoResizeColumn is called again.

The following code example demonstrates initializing a ListView in detail view and automatically resizing the columns using the AutoResizeColumn method. To run this example, paste this code into a Windows Form and call the InitializeResizingListView method from the form's constructor or Load event handler.

private ListView resizingListView = new ListView();
private Button button1 = new Button();

private void InitializeResizingListView()
{
    // Set location and text for button.
    button1.Location = new Point(100, 15);
    button1.Text = "Resize";
    button1.Click += new EventHandler(button1_Click);

    // Set the ListView to details view.
    resizingListView.View = View.Details;

    //Set size, location and populate the ListView.
    resizingListView.Size = new Size(200, 100);
    resizingListView.Location = new Point(40, 40);
    resizingListView.Columns.Add("HeaderSize");
    resizingListView.Columns.Add("ColumnContent");
    ListViewItem listItem1 = new ListViewItem("Short");
    ListViewItem listItem2 = new ListViewItem("Tiny");
    listItem1.SubItems.Add(new ListViewItem.ListViewSubItem( 
            listItem1, "Something longer"));
    listItem2.SubItems.Add(new ListViewItem.ListViewSubItem(
        listItem2, "Something even longer"));
    resizingListView.Items.Add(listItem1);
    resizingListView.Items.Add(listItem2);

    // Add the ListView and the Button to the form.
    this.Controls.Add(resizingListView);
    this.Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
    resizingListView.AutoResizeColumn(0, 
        ColumnHeaderAutoResizeStyle.HeaderSize);
    resizingListView.AutoResizeColumn(1, 
        ColumnHeaderAutoResizeStyle.ColumnContent);
}


Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft