Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ListView.ColumnHeaderCollection Class

Represents the collection of column headers in a ListView control.

System.Object
  System.Windows.Forms.ListView.ColumnHeaderCollection

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[ListBindableAttribute(false)]
public class ColumnHeaderCollection : IList, 
	ICollection, IEnumerable

The ListView.ColumnHeaderCollection type exposes the following members.

  NameDescription
Public methodListView.ColumnHeaderCollectionInitializes a new instance of the ListView.ColumnHeaderCollection class.
Top
  NameDescription
Public propertyCountGets the number of items in the collection.
Public propertyIsReadOnlyGets a value indicating whether the collection is read-only.
Public propertyItem[Int32]Gets the column header at the specified index within the collection.
Public propertyItem[String]Gets the column header with the specified key from the collection.
Top
  NameDescription
Public methodAdd(ColumnHeader)Adds an existing ColumnHeader to the collection.
Public methodAdd(String)Creates and adds a column with the specified text to the collection.
Public methodAdd(String, Int32)Creates and adds a column with the specified text and width to the collection.
Public methodAdd(String, String)Creates and adds a column with the specified text and key to the collection.
Public methodAdd(String, Int32, HorizontalAlignment)Adds a column header to the collection with specified text, width, and alignment settings.
Public methodAdd(String, String, Int32)Creates and adds a column with the specified text, key, and width to the collection.
Public methodAdd(String, String, Int32, HorizontalAlignment, Int32)Creates and adds a column with the specified key, aligned text, width, and image index to the collection.
Public methodAdd(String, String, Int32, HorizontalAlignment, String)Creates and adds a column with the specified key, aligned text, width, and image key to the collection.
Public methodAddRangeAdds an array of column headers to the collection.
Public methodClearRemoves all column headers from the collection.
Public methodContainsDetermines whether the specified column header is located in the collection.
Public methodContainsKeyDetermines if a column with the specified key is contained in the collection.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator to use to iterate through the column header collection.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfReturns the index, within the collection, of the specified column header.
Public methodIndexOfKeyDetermines the index for a column with the specified key.
Public methodInsert(Int32, String)Creates a new column header with the specified text, and inserts the header into the collection at the specified index.
Public methodInsert(Int32, ColumnHeader)Inserts an existing column header into the collection at the specified index.
Public methodInsert(Int32, String, Int32)Creates a new column header with the specified text and initial width, and inserts the header into the collection at the specified index.
Public methodInsert(Int32, String, String)Creates a new column header with the specified text and key, and inserts the header into the collection at the specified index.
Public methodInsert(Int32, String, Int32, HorizontalAlignment)Creates a new column header and inserts it into the collection at the specified index.
Public methodInsert(Int32, String, String, Int32)Creates a new column header with the specified text, key, and width, and inserts the header into the collection at the specified index.
Public methodInsert(Int32, String, String, Int32, HorizontalAlignment, Int32)Creates a new column header with the specified aligned text, key, width, and image index, and inserts the header into the collection at the specified index.
Public methodInsert(Int32, String, String, Int32, HorizontalAlignment, String)Creates a new column header with the specified aligned text, key, width, and image key, and inserts the header into the collection at the specified index.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified column header from the collection.
Public methodRemoveAtRemoves the column header at the specified index within the collection.
Public methodRemoveByKeyRemoves the column with the specified key from the collection.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToInfrastructure. Copies the ColumnHeader objects in the ListView.ColumnHeaderCollection to an array, starting at a particular array index.
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedInfrastructure. Gets a value indicating whether access to the ListView.ColumnHeaderCollection is synchronized (thread safe).
Explicit interface implemetationPrivate propertyICollection.SyncRootInfrastructure. Gets an object that can be used to synchronize access to the collection of controls.
Explicit interface implemetationPrivate methodIList.AddInfrastructure. Adds a ColumnHeader to the ListView.
Explicit interface implemetationPrivate methodIList.ContainsInfrastructure. Determines whether the specified column header is located in the collection.
Explicit interface implemetationPrivate methodIList.IndexOfInfrastructure. Returns the index, within the collection, of the specified column header.
Explicit interface implemetationPrivate methodIList.InsertInfrastructure. Inserts an existing column header into the collection at the specified index.
Explicit interface implemetationPrivate propertyIList.IsFixedSizeInfrastructure. Gets a value indicating whether the ListView.ColumnHeaderCollection has a fixed size.
Explicit interface implemetationPrivate propertyIList.ItemInfrastructure. Gets or sets the column header at the specified index within the collection.
Explicit interface implemetationPrivate methodIList.RemoveInfrastructure. Removes the specified column header from the collection.
Top

A ListView.ColumnHeaderCollection class stores the column headers that are displayed in the ListView control when the View property is set to Details. The ListView.ColumnHeaderCollection stores ColumnHeader objects that define the text to display for a column as well as how the column header is displayed in the ListView control when displaying columns. When a ListView displays columns, the items and their subitems are displayed in their own columns. To specify which columns subitem data is displayed under, see the ListViewItem.ListViewSubItemCollection class.

There are a number of ways to add column headers to the collection. The Add method adds a single column header to the collection. To add a number of column headers to the collection, you create an array of ColumnHeader objects and pass it to the AddRange method. If you want to insert a column header at a specific location in the collection, you can use the Insert method. To remove column headers, you can use either the Remove method or the RemoveAt method if you know where the column header is located in the collection. The Clear method allows you to remove all column headers from the collection instead of using the Remove method to remove a single column header at a time.

In addition to methods and properties for adding and removing column headers, the ListView.ColumnHeaderCollection also provides methods to find column headers in the collection. The Contains method enables you to determine whether a column header is a member of the collection. Once you know that the column header is located in the collection, you can use the IndexOf method to determine where the column header is located in the collection.

NoteNote

Unexpected behavior can result when the combined width of all columns exceeds 32,768 pixels.

The following code example creates a form that contains a ListView control that manually sorts items when a column in the ListView control is clicked. The example defines a class called ListViewItemComparer that implements the System.Collections.IComparer interface that performs the ListViewItem comparison. The example creates an instance of ListViewItemComparer and uses it to set the ListViewItemSorter property of the ListView control. The Sort method call in the ColumnClick event handler uses the methods defined in ListViewItemComparer to perform the sort of items, based on the column that is clicked.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace ListViewSortFormNamespace
{


    public class ListViewSortForm : Form
    {
        private ListView listView1;

        public ListViewSortForm()
        {
            // Create ListView items to add to the control.
            ListViewItem listViewItem1 = new ListViewItem(new string[] {"Banana","a","b","c"}, -1, Color.Empty, Color.Yellow, null);
            ListViewItem listViewItem2 = new ListViewItem(new string[] {"Cherry","v","g","t"}, -1, Color.Empty, Color.Red, new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))));
            ListViewItem listViewItem3 = new ListViewItem(new string[] {"Apple","h","j","n"}, -1, Color.Empty, Color.Lime, null);
            ListViewItem listViewItem4 = new ListViewItem(new string[] {"Pear","y","u","i"}, -1, Color.Empty, Color.FromArgb(((System.Byte)(192)), ((System.Byte)(128)), ((System.Byte)(156))), null);

            //Initialize the ListView control and add columns to it. 
            this.listView1 = new ListView();

            // Set the initial sorting type for the ListView. 
            this.listView1.Sorting = SortOrder.None;
            // Disable automatic sorting to enable manual sorting. 
            this.listView1.View = View.Details;
            // Add columns and set their text. 
            this.listView1.Columns.Add(new ColumnHeader());
            this.listView1.Columns[0].Text = "Column 1";
            this.listView1.Columns[0].Width = 100;
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[1].Text = "Column 2";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[2].Text = "Column 3";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[3].Text = "Column 4";
            // Suspend control logic until form is done configuring form. 
            this.SuspendLayout();
            // Add Items to the ListView control. 
            this.listView1.Items.AddRange(new ListViewItem[] {listViewItem1,
                listViewItem2,
                listViewItem3,
                listViewItem4});
            // Set the location and size of the ListView control. 
            this.listView1.Location = new Point(10, 10);
            this.listView1.Name = "listView1";
            this.listView1.Size = new Size(300, 100);
            this.listView1.TabIndex = 0;
            // Enable editing of the items in the ListView. 
            this.listView1.LabelEdit = true;
            // Connect the ListView.ColumnClick event to the ColumnClick event handler. 
            this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick);
   			
            // Initialize the form. 
            this.ClientSize = new Size(400, 400);
            this.Controls.AddRange(new Control[] {this.listView1});
            this.Name = "ListViewSortForm";
            this.Text = "Sorted ListView Control";
            // Resume layout of the form. 
            this.ResumeLayout(false);
        }

	
        // ColumnClick event handler. 
        private void ColumnClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer  
            // object. Setting this property immediately sorts the  
            // ListView using the ListViewItemComparer object. 
            this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }

        [System.STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new ListViewSortForm());
        }

    }

    // Implements the manual sorting of items by columns. 
    class ListViewItemComparer : IComparer
    {
        private int col;
        public ListViewItemComparer()
        {
            col = 0;
        }
        public ListViewItemComparer(int column)
        {
            col = column;
        }
        public int Compare(object x, object y)
        {
            return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
    }

}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.