public AutoResizeColumns ( ColumnHeaderAutoResizeStyle headerAutoResize ) : void | ||
headerAutoResize | ColumnHeaderAutoResizeStyle | |
return | void |
// Create a new ListView control ListView listView1 = new ListView(); // Add columns to the ListView control listView1.Columns.Add("Column 1", 100); listView1.Columns.Add("Column 2", 100); listView1.Columns.Add("Column 3", 100); // Add items to the ListView control listView1.Items.Add(new ListViewItem(new string[] { "Item 1", "Item 2", "Item 3" })); listView1.Items.Add(new ListViewItem(new string[] { "Item 4", "Item 5", "Item 6" })); listView1.Items.Add(new ListViewItem(new string[] { "Item 7", "Item 8", "Item 9" })); // AutoResizeColumns method call to adjust column widths listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
ListView listView2 = new ListView(); ColumnHeader columnHeader1 = new ColumnHeader(); ColumnHeader columnHeader2 = new ColumnHeader(); ColumnHeader columnHeader3 = new ColumnHeader(); columnHeader1.Text = "Column 1"; columnHeader1.Width = -1; //Set to -1 to set column width automatically. columnHeader2.Text = "Column 2"; columnHeader2.Width = -1; columnHeader3.Text = "Column 3"; columnHeader3.Width = -1; listView2.Columns.AddRange(new ColumnHeader[] { columnHeader1, columnHeader2, columnHeader3 }); listView2.Items.Add(new ListViewItem(new string[] { "Item 1-1", "Item 1-2"})); listView2.Items.Add(new ListViewItem(new string[] { "Item 2-1", "Item 2-2", "Item 2-3"})); listView2.Items.Add(new ListViewItem(new string[] { "Item 3-1", "Item 3-2", "Item 3-3", "Item 3-4"})); listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);In this example, we create a new ListView control, add columns and items, and then call the AutoResizeColumns method to adjust the column widths based on the header size of each column. The ColumnHeaderAutoResizeStyle.HeaderSize parameter specifies that the method adjusts the columns to fit the headers. The package library for System.Windows.Forms.ListView AutoResizeColumns method is the .NET Framework library.
public AutoResizeColumns ( ColumnHeaderAutoResizeStyle headerAutoResize ) : void | ||
headerAutoResize | ColumnHeaderAutoResizeStyle | |
return | void |