ColumnHeader Class
Displays a single column header in a ListView control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
A column header is an item in a ListView control that contains heading text. ColumnHeader objects can be added to a ListView using the Add method of the ListView.ColumnHeaderCollection class. To add a group of columns to a ListView, you can use the AddRange method of the ListView.ColumnHeaderCollection class. You can use the Index property of the ColumnHeader class to determine where the ColumnHeader is located in the ListView.ColumnHeaderCollection.
ColumnHeader provides the Text and TextAlign properties to set the text displayed in the control and the alignment of the text in the column header. To determine whether a ColumnHeader is associated with a ListView control, you can reference the ListView property. If you want to copy a ColumnHeader for use in another ListView control, you can use the Clone method.
The following code example demonstrates initializing a ListView control. The example creates ColumnHeader objects and sets the column header's Text, TextAlign and Width properties. The example also adds items and subitems to the ListView. To run this example paste the following code in a form and call the PopulateListView method from the form's constructor or Load event handler.
Private Sub PopulateListView() ListView1.Width = 270 ListView1.Location = New System.Drawing.Point(10, 10) ' Declare and construct the ColumnHeader objects. Dim header1, header2 As ColumnHeader header1 = New ColumnHeader header2 = New ColumnHeader ' Set the text, alignment and width for each column header. header1.Text = "File name" header1.TextAlign = HorizontalAlignment.Left header1.Width = 70 header2.TextAlign = HorizontalAlignment.Left header2.Text = "Location" header2.Width = 200 ' Add the headers to the ListView control. ListView1.Columns.Add(header1) ListView1.Columns.Add(header2) ' Populate the ListView.Items property. ' Set the directory to the sample picture directory. Dim dirInfo As New System.IO.DirectoryInfo _ ("C:\Documents and Settings\All Users" _ & "\Documents\My Pictures\Sample Pictures") Dim file As System.IO.FileInfo ' Get the .jpg files from the directory Dim files() As System.io.FileInfo = dirInfo.GetFiles("*.jpg") ' Add each file name and full name including path ' to the ListView. If (files IsNot Nothing) Then For Each file In files Dim item As New ListViewItem(file.Name) item.SubItems.Add(file.FullName) ListView1.Items.Add(item) Next End If End Sub
The example creates ColumnHeader objects and sets the column header's Text, TextAlign and Width properties. The example also adds items and subitems to the ListView. To run this example paste the following code in a form and call the PopulateListView method from the form's constructor or Load method.
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.ColumnHeader
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.