实例代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace ListViewDemo
...{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DemoForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.ListView lvw;
private System.Windows.Forms.ImageList ilsLargeIcons;
private System.Windows.Forms.ImageList ilsSmallIcons;
private System.Windows.Forms.ComboBox cboView;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button btnUse;
private System.Windows.Forms.Button btnLeave;
private ListViewItem itmX;
private System.Windows.Forms.Button btnStat;
const int MoneyPerHour=2;
public DemoForm()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));
this.lvw = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.ilsLargeIcons = new System.Windows.Forms.ImageList(this.components);
this.ilsSmallIcons = new System.Windows.Forms.ImageList(this.components);
this.cboView = new System.Windows.Forms.ComboBox();
this.btnUse = new System.Windows.Forms.Button();
this.btnLeave = new System.Windows.Forms.Button();
this.btnStat = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lvw
//
this.lvw.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] ...{
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6});
this.lvw.FullRowSelect = true;
this.lvw.GridLines = true;
this.lvw.HideSelection = false;
this.lvw.LargeImageList = this.ilsLargeIcons;
this.lvw.Location = new System.Drawing.Point(0, 0);
this.lvw.MultiSelect = false;
this.lvw.Name = "lvw";
this.lvw.Size = new System.Drawing.Size(512, 232);
this.lvw.TabIndex = 0;
this.lvw.View = System.Windows.Forms.View.Details;
this.lvw.SelectedIndexChanged += new System.EventHandler(this.lvw_SelectedIndexChanged);
//
// columnHeader1
//
this.columnHeader1.Text = "机号";
this.columnHeader1.Width = 54;
//
// columnHeader2
//
this.columnHeader2.Text = "状态";
this.columnHeader2.Width = 58;
//
// columnHeader3
//
this.columnHeader3.Text = "上机时间";
this.columnHeader3.Width = 126;
//
// columnHeader4
//
this.columnHeader4.Text = "下机时间";
this.columnHeader4.Width = 118;
//
// columnHeader5
//
this.columnHeader5.Text = "小计";
this.columnHeader5.Width = 70;
//
// columnHeader6
//
this.columnHeader6.Text = "金额";
//
// ilsLargeIcons
//
this.ilsLargeIcons.ImageSize = new System.Drawing.Size(32, 32);
this.ilsLargeIcons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilsLargeIcons.ImageStream")));
this.ilsLargeIcons.TransparentColor = System.Drawing.Color.Transparent;
//
// ilsSmallIcons
//
this.ilsSmallIcons.ImageSize = new System.Drawing.Size(16, 16);
this.ilsSmallIcons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilsSmallIcons.ImageStream")));
this.ilsSmallIcons.TransparentColor = System.Drawing.Color.Transparent;
//
// cboView
//
this.cboView.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboView.Items.AddRange(new object[] ...{
"大图标",
"详细资料",
"小图标",
"列表"});
this.cboView.Location = new System.Drawing.Point(24, 248);
this.cboView.Name = "cboView";
this.cboView.Size = new System.Drawing.Size(96, 20);
this.cboView.TabIndex = 1;
this.cboView.SelectedIndexChanged += new System.EventHandler(this.cboView_SelectedIndexChanged);
//
// btnUse
//
this.btnUse.Location = new System.Drawing.Point(168, 248);
this.btnUse.Name = "btnUse";
this.btnUse.Size = new System.Drawing.Size(80, 24);
this.btnUse.TabIndex = 2;
this.btnUse.Text = "上机登记";
this.btnUse.Click += new System.EventHandler(this.btnUse_Click);
//
// btnLeave
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace ListViewDemo
...{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DemoForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.ListView lvw;
private System.Windows.Forms.ImageList ilsLargeIcons;
private System.Windows.Forms.ImageList ilsSmallIcons;
private System.Windows.Forms.ComboBox cboView;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button btnUse;
private System.Windows.Forms.Button btnLeave;
private ListViewItem itmX;
private System.Windows.Forms.Button btnStat;
const int MoneyPerHour=2;
public DemoForm()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));
this.lvw = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.ilsLargeIcons = new System.Windows.Forms.ImageList(this.components);
this.ilsSmallIcons = new System.Windows.Forms.ImageList(this.components);
this.cboView = new System.Windows.Forms.ComboBox();
this.btnUse = new System.Windows.Forms.Button();
this.btnLeave = new System.Windows.Forms.Button();
this.btnStat = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lvw
//
this.lvw.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] ...{
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6});
this.lvw.FullRowSelect = true;
this.lvw.GridLines = true;
this.lvw.HideSelection = false;
this.lvw.LargeImageList = this.ilsLargeIcons;
this.lvw.Location = new System.Drawing.Point(0, 0);
this.lvw.MultiSelect = false;
this.lvw.Name = "lvw";
this.lvw.Size = new System.Drawing.Size(512, 232);
this.lvw.TabIndex = 0;
this.lvw.View = System.Windows.Forms.View.Details;
this.lvw.SelectedIndexChanged += new System.EventHandler(this.lvw_SelectedIndexChanged);
//
// columnHeader1
//
this.columnHeader1.Text = "机号";
this.columnHeader1.Width = 54;
//
// columnHeader2
//
this.columnHeader2.Text = "状态";
this.columnHeader2.Width = 58;
//
// columnHeader3
//
this.columnHeader3.Text = "上机时间";
this.columnHeader3.Width = 126;
//
// columnHeader4
//
this.columnHeader4.Text = "下机时间";
this.columnHeader4.Width = 118;
//
// columnHeader5
//
this.columnHeader5.Text = "小计";
this.columnHeader5.Width = 70;
//
// columnHeader6
//
this.columnHeader6.Text = "金额";
//
// ilsLargeIcons
//
this.ilsLargeIcons.ImageSize = new System.Drawing.Size(32, 32);
this.ilsLargeIcons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilsLargeIcons.ImageStream")));
this.ilsLargeIcons.TransparentColor = System.Drawing.Color.Transparent;
//
// ilsSmallIcons
//
this.ilsSmallIcons.ImageSize = new System.Drawing.Size(16, 16);
this.ilsSmallIcons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilsSmallIcons.ImageStream")));
this.ilsSmallIcons.TransparentColor = System.Drawing.Color.Transparent;
//
// cboView
//
this.cboView.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboView.Items.AddRange(new object[] ...{
"大图标",
"详细资料",
"小图标",
"列表"});
this.cboView.Location = new System.Drawing.Point(24, 248);
this.cboView.Name = "cboView";
this.cboView.Size = new System.Drawing.Size(96, 20);
this.cboView.TabIndex = 1;
this.cboView.SelectedIndexChanged += new System.EventHandler(this.cboView_SelectedIndexChanged);
//
// btnUse
//
this.btnUse.Location = new System.Drawing.Point(168, 248);
this.btnUse.Name = "btnUse";
this.btnUse.Size = new System.Drawing.Size(80, 24);
this.btnUse.TabIndex = 2;
this.btnUse.Text = "上机登记";
this.btnUse.Click += new System.EventHandler(this.btnUse_Click);
//
// btnLeave