WinForm ListView How to create a ColumnHeader

Source: Internet
Author: User

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) (()), ((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 the done configuring form. This.            SuspendLayout (); Add Items to the ListView contRol. This.listView1.Items.AddRange (new listviewitem[] {listViewItem1, listViewItem2, ListViewItem            3, 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 LISTVIEWSO        Rtform ());    }}//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); }    }}

WinForm ListView method for creating ColumnHeader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.