实例代码如下:
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 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DemoForm());
}
private void DemoForm_Load(object sender, System.EventArgs e)
{
lvw.SmallImageList=ilsSmallIcons;
for (int i=1;i<=30;i++)
{
itmX=lvw.Items.Add(i.ToString(),0); //Add方法返回新增的ListViewItem对象
itmX.SubItems.Add("未使用");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
}
cboView.SelectedIndex=1;
}
private void cboView_SelectedIndexChanged(object sender, System.EventArgs e)
{
lvw.View=(View)cboView.SelectedIndex;
}
private void btnUse_Click(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
itmX=lvw.SelectedItems[0];
if (MessageBox.Show("确定使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
itmX.SubItems[2].Text=DateTime.Now.ToString();
itmX.SubItems[1].Text="使用中";
itmX.ImageIndex=1;
itmX.SubItems[3].Text="";
itmX.SubItems[4].Text="";
itmX.SubItems[5].Text="";
lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
}
}
}
private void btnLeave_Click(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
itmX=lvw.SelectedItems[0];
if (MessageBox.Show("确定不再使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
itmX.SubItems[3].Text=DateTime.Now.ToString();
itmX.SubItems[1].Text="未使用";
itmX.ImageIndex=0;
TimeSpan ts=DateTime.Now-DateTime.Parse(itmX.SubItems[2].Text);
itmX.SubItems[4].Text=ts.Hours + "小时" + ts.Minutes + "分";
int hours=ts.Hours;
if (ts.Minutes>0) hours++;//不足一小时按一小时计算
itmX.SubItems[5].Text=hours*MoneyPerHour + "元";
lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
SqlConnection cn=DBOperate.GetConnection();
try
{
cn.Open();
SqlCommand cmd=new SqlCommand("Insert into UsingDetails Values(@ComputerNo, @StartTime, @EndTime, @TotalMoney)",cn);
cmd.Parameters.Add("@ComputerNo",int.Parse(itmX.Text));
cmd.Parameters.Add("@StartTime",DateTime.Parse(itmX.SubItems[2].Text));
cmd.Parameters.Add("@EndTime",DateTime.Now);
cmd.Parameters.Add("@TotalMoney",(decimal)(hours*MoneyPerHour));
cmd.ExecuteNonQuery();
}
finally
{
cn.Close();
}
}
}
}
private void btnStat_Click(object sender, System.EventArgs e)
{
StatForm frmStat=new StatForm();
frmStat.ShowDialog();
}
private void lvw_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
if (lvw.SelectedItems[0].ImageIndex==0)
{
btnUse.Enabled=true;
btnLeave.Enabled=false;
}
else
{
btnUse.Enabled=false;
btnLeave.Enabled=true;
}
}
}
}
}
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 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DemoForm());
}
private void DemoForm_Load(object sender, System.EventArgs e)
{
lvw.SmallImageList=ilsSmallIcons;
for (int i=1;i<=30;i++)
{
itmX=lvw.Items.Add(i.ToString(),0); //Add方法返回新增的ListViewItem对象
itmX.SubItems.Add("未使用");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
itmX.SubItems.Add("");
}
cboView.SelectedIndex=1;
}
private void cboView_SelectedIndexChanged(object sender, System.EventArgs e)
{
lvw.View=(View)cboView.SelectedIndex;
}
private void btnUse_Click(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
itmX=lvw.SelectedItems[0];
if (MessageBox.Show("确定使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
itmX.SubItems[2].Text=DateTime.Now.ToString();
itmX.SubItems[1].Text="使用中";
itmX.ImageIndex=1;
itmX.SubItems[3].Text="";
itmX.SubItems[4].Text="";
itmX.SubItems[5].Text="";
lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
}
}
}
private void btnLeave_Click(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
itmX=lvw.SelectedItems[0];
if (MessageBox.Show("确定不再使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
itmX.SubItems[3].Text=DateTime.Now.ToString();
itmX.SubItems[1].Text="未使用";
itmX.ImageIndex=0;
TimeSpan ts=DateTime.Now-DateTime.Parse(itmX.SubItems[2].Text);
itmX.SubItems[4].Text=ts.Hours + "小时" + ts.Minutes + "分";
int hours=ts.Hours;
if (ts.Minutes>0) hours++;//不足一小时按一小时计算
itmX.SubItems[5].Text=hours*MoneyPerHour + "元";
lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
SqlConnection cn=DBOperate.GetConnection();
try
{
cn.Open();
SqlCommand cmd=new SqlCommand("Insert into UsingDetails Values(@ComputerNo, @StartTime, @EndTime, @TotalMoney)",cn);
cmd.Parameters.Add("@ComputerNo",int.Parse(itmX.Text));
cmd.Parameters.Add("@StartTime",DateTime.Parse(itmX.SubItems[2].Text));
cmd.Parameters.Add("@EndTime",DateTime.Now);
cmd.Parameters.Add("@TotalMoney",(decimal)(hours*MoneyPerHour));
cmd.ExecuteNonQuery();
}
finally
{
cn.Close();
}
}
}
}
private void btnStat_Click(object sender, System.EventArgs e)
{
StatForm frmStat=new StatForm();
frmStat.ShowDialog();
}
private void lvw_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (lvw.SelectedItems.Count>0)
{
if (lvw.SelectedItems[0].ImageIndex==0)
{
btnUse.Enabled=true;
btnLeave.Enabled=false;
}
else
{
btnUse.Enabled=false;
btnLeave.Enabled=true;
}
}
}
}
}
这个实例基本上包含了ListView常用方法和属性,最主要的就是要明白ListView中包含的都是ListViewItem对象。