Set ColumnHeader for ListView

image_pdfimage_print

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(464, 152);
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(75, 0);
this.REPORT.Name = “REPORT”;
this.REPORT.Size = new System.Drawing.Size(75, 24);
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(300, 0);
this.LIST.Name = “LIST”;
this.LIST.Size = new System.Drawing.Size(75, 24);
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(150, 0);
this.SMALLICON.Name = “SMALLICON”;
this.SMALLICON.Size = new System.Drawing.Size(75, 24);
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(375, 0);
this.LARGEICON.Name = “LARGEICON”;
this.LARGEICON.Size = new System.Drawing.Size(75, 24);
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(75, 24);
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(225, 0);
this.CHECKED.Name = “CHECKED”;
this.CHECKED.Size = new System.Drawing.Size(75, 24);
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(0, 157);
this.panel1.Name = “panel1”;
this.panel1.Size = new System.Drawing.Size(464, 24);
this.panel1.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 181);
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(); } } [/csharp]