Set ColumnHeader for ListView : ListView « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ListViewScreenshots 
Set ColumnHeader for ListView
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.ListView listView1 = new System.Windows.Forms.ListView();
    private System.Windows.Forms.ColumnHeader Country= new System.Windows.Forms.ColumnHeader();
    private System.Windows.Forms.ColumnHeader Capital= new System.Windows.Forms.ColumnHeader();
    private System.Windows.Forms.ColumnHeader City_1= new System.Windows.Forms.ColumnHeader();
    private System.Windows.Forms.ColumnHeader City_2= new System.Windows.Forms.ColumnHeader();
    private System.Windows.Forms.Button REPORT= new System.Windows.Forms.Button();
    private System.Windows.Forms.Button LIST= new System.Windows.Forms.Button();
    private System.Windows.Forms.Button SMALLICON= new System.Windows.Forms.Button();
    private System.Windows.Forms.Button LARGEICON= new System.Windows.Forms.Button();
    ImageList ig = new ImageList();
    private System.Windows.Forms.Button SELECTED= new System.Windows.Forms.Button();
    private System.Windows.Forms.Button CHECKED= new System.Windows.Forms.Button();
    private System.Windows.Forms.Panel panel1= new System.Windows.Forms.Panel();
    public Form1() {
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        this.listView1.AllowColumnReorder = true;
        this.listView1.CheckBoxes = true;
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                    this.Country,
                                                                                    this.Capital,
                                                                                    this.City_1,
                                                                                    this.City_2});
        this.listView1.Dock = System.Windows.Forms.DockStyle.Top;
        this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif"11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.listView1.ForeColor = System.Drawing.SystemColors.HotTrack;
        this.listView1.FullRowSelect = true;
        this.listView1.Size = new System.Drawing.Size(464152);
        this.listView1.TabIndex = 0;
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
        this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
        // 
        // Country
        // 
        this.Country.Text = "Country";
        this.Country.Width = 100;
        // 
        // Capital
        // 
        this.Capital.Text = "Capital";
        this.Capital.Width = 150;
        // 
        // City_1
        // 
        this.City_1.Text = "City_1";
        this.City_1.Width = 100;
        // 
        // City_2
        // 
        this.City_2.Text = "City_2";
        this.City_2.Width = 100;
        // 
        // REPORT
        // 
        this.REPORT.Dock = System.Windows.Forms.DockStyle.Left;
        this.REPORT.Location = new System.Drawing.Point(750);
        this.REPORT.Name = "REPORT";
        this.REPORT.Size = new System.Drawing.Size(7524);
        this.REPORT.TabIndex = 1;
        this.REPORT.Text = "Report";
        this.REPORT.Click += new System.EventHandler(this.REPORT_Click);
        // 
        // LIST
        // 
        this.LIST.Dock = System.Windows.Forms.DockStyle.Left;
        this.LIST.Location = new System.Drawing.Point(3000);
        this.LIST.Name = "LIST";
        this.LIST.Size = new System.Drawing.Size(7524);
        this.LIST.TabIndex = 1;
        this.LIST.Text = "List";
        this.LIST.Click += new System.EventHandler(this.LIST_Click);
        // 
        // SMALLICON
        // 
        this.SMALLICON.Dock = System.Windows.Forms.DockStyle.Left;
        this.SMALLICON.Location = new System.Drawing.Point(1500);
        this.SMALLICON.Name = "SMALLICON";
        this.SMALLICON.Size = new System.Drawing.Size(7524);
        this.SMALLICON.TabIndex = 1;
        this.SMALLICON.Text = "Small Icon";
        this.SMALLICON.Click += new System.EventHandler(this.SMALLICON_Click);
        // 
        // LARGEICON
        // 
        this.LARGEICON.Dock = System.Windows.Forms.DockStyle.Left;
        this.LARGEICON.Location = new System.Drawing.Point(3750);
        this.LARGEICON.Name = "LARGEICON";
        this.LARGEICON.Size = new System.Drawing.Size(7524);
        this.LARGEICON.TabIndex = 1;
        this.LARGEICON.Text = "Large Icon";
        this.LARGEICON.Click += new System.EventHandler(this.LARGEICON_Click);
        // 
        // SELECTED
        // 
        this.SELECTED.Dock = System.Windows.Forms.DockStyle.Left;
        this.SELECTED.Name = "SELECTED";
        this.SELECTED.Size = new System.Drawing.Size(7524);
        this.SELECTED.TabIndex = 2;
        this.SELECTED.Text = "Selected";
        this.SELECTED.Click += new System.EventHandler(this.SELECTED_Click);
        // 
        // CHECKED
        // 
        this.CHECKED.Dock = System.Windows.Forms.DockStyle.Left;
        this.CHECKED.Location = new System.Drawing.Point(2250);
        this.CHECKED.Name = "CHECKED";
        this.CHECKED.Size = new System.Drawing.Size(7524);
        this.CHECKED.TabIndex = 3;
        this.CHECKED.Text = "Checked";
        this.CHECKED.Click += new System.EventHandler(this.CHECKED_Click);
        // 
        // panel1
        // 
        this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                             this.LARGEICON,
                                                                             this.LIST,
                                                                             this.CHECKED,
                                                                             this.SMALLICON,
                                                                             this.REPORT,
                                                                             this.SELECTED});
        this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.panel1.Location = new System.Drawing.Point(0157);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(46424);
        this.panel1.TabIndex = 4;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(464181);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.panel1,
                                                                      this.listView1});
        this.Name = "Form1";
        this.Text = "ListView";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.panel1.ResumeLayout(false);
        this.ResumeLayout(false);

    }


    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
        ig.Images.Add(new Icon("FLGUSA01.ICO"));
        ig.Images.Add(new Icon("CTRITALY.ICO"));
        ig.Images.Add(new Icon("FLGCAN.ICO"));
        ig.Images.Add(new Icon("FLGSWITZ.ICO"));
        ig.Images.Add(new Icon("FLGUK.ICO"));

        listView1.SmallImageList = ig;
        listView1.LargeImageList = ig;

        listView1.Columns[1].TextAlign = HorizontalAlignment.Center;
        listView1.Columns[2].TextAlign = HorizontalAlignment.Center;
        listView1.Columns[3].TextAlign = HorizontalAlignment.Center;

        string[] lv = new String[4];
        lv[0"A";
        lv[1"A1";
        lv[2"A2";
        lv[3"A3";
        listView1.Items.Add(new ListViewItem(lv, 0));

        lv[0"B";
        lv[1"B1";
        lv[2"B2";
        lv[3"B3";
        listView1.Items.Add(new ListViewItem(lv, 1));

        lv[0"C";
        lv[1"C1";
        lv[2"C2";
        lv[3"C3";
        listView1.Items.Add(new ListViewItem(lv, 2));

        lv[0"D";
        lv[1"D1";
        lv[2"D2";
        lv[3"D3";
        listView1.Items.Add(new ListViewItem(lv, 3));

        lv[0"E";
        lv[1"E1";
        lv[2"E2";
        lv[3"E3";
        listView1.Items.Add(new ListViewItem(lv, 4));

        for (int j = 0; j < listView1.Items.Count; j++) {
            ListViewItem lvi = listView1.Items[j];
            for (int i = 0; i < lvi.SubItems.Count; i++) {
                Console.WriteLine(lvi.SubItems[i].Text);
            }
        }
    }

    private void REPORT_Click(object sender, System.EventArgs e) {
        this.Text = "Report View";
        listView1.View = View.Details;
    }

    private void LARGEICON_Click(object sender, System.EventArgs e) {
        this.Text = "LargeIcon View";
        listView1.View = View.LargeIcon;
    }

    private void SMALLICON_Click(object sender, System.EventArgs e) {
        this.Text = "SmallIcon View";
        listView1.View = View.SmallIcon;
    }

    private void LIST_Click(object sender, System.EventArgs e) {
        this.Text = "List View";
        listView1.View = View.List;
    }

    private void SELECTED_Click(object sender, System.EventArgs e) {
        for (int k = 0; k < listView1.SelectedIndices.Count; k++) {
            ListViewItem lvi = listView1.Items[listView1.SelectedIndices[k]];
            for (int i = 0; i < lvi.SubItems.Count; i++) {
                Console.WriteLine(lvi.SubItems[i].Text);
            }
        }
    }

    private void CHECKED_Click(object sender, System.EventArgs e) {
        for (int k = 0; k < listView1.CheckedIndices.Count; k++) {
            ListViewItem lvi = listView1.Items[listView1.CheckedIndices[k]];
            for (int i = 0; i < lvi.SubItems.Count; i++) {
                Console.WriteLine(lvi.SubItems[i].Text);
            }
        }
    }

    private void listView1_SelectedIndexChanged(object sender, System.EventArgs e) {

    }

    private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e) {
        if (listView1.Sorting == SortOrder.Descending)
            listView1.Sorting = SortOrder.Ascending;
        else
            listView1.Sorting = SortOrder.Descending;
        listView1.Sort();
    }


}

 
Related examples in the same category
1.Windows Explorer-Like Program: extends ListView
2.Dragging and dropping between ListView
3.ListView Item clicked eventListView Item clicked event
4.Folder Browser based on ListViewFolder Browser based on ListView
5.Sort a List View by Any ColumnSort a List View by Any Column
6.Add Data to a ListView
7.Extends ListViewItem
8.Use RadioButton to control the ListView display styleUse RadioButton to control the ListView display style
9.Display Directory info in a ListViewDisplay Directory info in a ListView
10.Add ListView column and insert ListView rowsAdd ListView column and insert ListView rows
11.Use ListViewItem to display file information
12.ListView ExampleListView Example
13.ListView Country: image and fontListView Country: image and font
14.Use ListView to display File info: name, size and dateUse ListView to display File info: name, size and date
15.Use ListView to display file name and double click the name to execute that fileUse ListView to display file name and double click the name to execute that file
16.Use ListView to diaplay folder info and double click to enter that directoryUse ListView to diaplay folder info and double click to enter that directory
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.