//KeyWord:ListView 使用方法属性说明
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace GreenMails_ListView
{
public partial class frmMail : Form
{
int i = 0;
/// <summary>
/// 窗体初始化
/// </summary>
public frmMail()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMail_Load(object sender, EventArgs e)
{
this.lvwMail.Columns.Add("发件人",80,HorizontalAlignment.Center);
this.lvwMail.Columns.Add("收件人",80,HorizontalAlignment.Center);
this.lvwMail.Columns.Add("主题",200,HorizontalAlignment.Center);
this.lvwMail.Columns.Add("日期",100,HorizontalAlignment.Center);
this.lvwMail.View = View.Details;
this.lvwMail.HeaderStyle = ColumnHeaderStyle.Nonclickable;
this.lvwMail.MultiSelect = false;
this.lvwMail.FullRowSelect = true;
this.lvwMail.GridLines = true;
GetMail("true");
}
/// <summary>
/// 获取记录并显示在ListView中
/// </summary>
/// <param name="type"></param>
private void GetMail(string type)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=GreenMail;Integrated Security=True");
SqlCommand cmd = new SqlCommand(string.Format("select * from Mails where Isdelete='{0}'",type), conn);
string id;
string receiver, accpetor, subject, content, time, isdelete;
conn.Open();
SqlDataReader dtr = cmd.ExecuteReader();
while (dtr.Read())
{
id = dtr[0].ToString();
receiver = dtr[1].ToString();
accpetor = dtr[2].ToString();
subject = dtr[3].ToString();
content = dtr[4].ToString();
time = dtr[5].ToString();
isdelete = dtr[5].ToString();
ListViewItem lviMail = new ListViewItem(receiver);
lviMail.Tag = id;
lvwMail.Items.Add(lviMail);
lviMail.SubItems.AddRange(new string[] { accpetor, subject, time });
}
dtr.Close();
conn.Close();
}
/// <summary>
/// “收件箱”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnReceive_Click(object sender, EventArgs e)
{
i = 0;
this.lvwMail.Items.Clear();
GetMail("true");
}
/// <summary>
/// “垃圾箱”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnDustbin_Click(object sender, EventArgs e)
{
i = 1;
this.lvwMail.Items.Clear();
GetMail("false");
}
/// <summary>
/// “删除”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnDelete_Click(object sender, EventArgs e)
{
if (this.lvwMail.SelectedItems.Count==0)
{
MessageBox.Show("没有选择任何一项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string mailID = this.lvwMail.SelectedItems[0].Tag.ToString();
//int mailID = (int)this.lvwMail.SelectedItems[0].Tag;
if (i==0)
{
string sql=string.Format("update Mails set isdelete='false' where id={0} ", mailID);
UpdateMails(sql);
}
else
{
DialogResult result = MessageBox.Show("确定要删除该邮件?", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result==DialogResult.Yes)
{
string sql = string.Format("delete from mails where id={0}", mailID);
UpdateMails(sql);
}
}
}
}
/// <summary>
/// 刷新邮件箱
/// </summary>
/// <param name="sql"></param>
private void UpdateMails(string sql)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=GreenMail;Integrated Security=True");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
this.lvwMail.SelectedItems[0].Remove();
conn.Close();
}
private void tsbtnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
//KeyWord:ListView 使用方法属性说明
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace GreenMails_ListView
{
public partial class frmMail : Form
{
int i = 0;
/// <summary>
/// 窗体初始化
/// </summary>
public frmMail()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMail_Load(object sender, EventArgs e)
{
this.lvwMail.Columns.Add("发件人");
this.lvwMail.Columns.Add("收件人");
this.lvwMail.Columns.Add("主题");
this.lvwMail.Columns.Add("日期");
this.lvwMail.Columns[0].DisplayIndex = 0;
this.lvwMail.Columns[0].Width = 80;
this.lvwMail.Columns[1].DisplayIndex = 1;
this.lvwMail.Columns[1].Width = 80;
this.lvwMail.Columns[2].DisplayIndex = 2;
this.lvwMail.Columns[2].Width = 200;
this.lvwMail.Columns[3].DisplayIndex = 3;
this.lvwMail.Columns[3].Width = 80;
GetMail("true");
}
/// <summary>
/// 获取记录并显示在ListView中
/// </summary>
/// <param name="type"></param>
private void GetMail(string type)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=GreenMail;Integrated Security=True");
SqlCommand cmd = new SqlCommand(string.Format("select * from Mails where Isdelete='{0}'",type), conn);
string id;
string receiver, accpetor, subject, content, time, isdelete;
conn.Open();
SqlDataReader dtr = cmd.ExecuteReader();
while (dtr.Read())
{
id = dtr[0].ToString();
receiver = dtr[1].ToString();
accpetor = dtr[2].ToString();
subject = dtr[3].ToString();
content = dtr[4].ToString();
time = dtr[5].ToString();
isdelete = dtr[5].ToString();
ListViewItem lviMail = new ListViewItem(receiver);
lviMail.Tag = id;
lvwMail.Items.Add(lviMail);
lviMail.SubItems.AddRange(new string[] { accpetor, subject, time });
}
dtr.Close();
conn.Close();
}
/// <summary>
/// “收件箱”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnReceive_Click(object sender, EventArgs e)
{
i = 0;
this.lvwMail.Items.Clear();
GetMail("true");
}
/// <summary>
/// “垃圾箱”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnDustbin_Click(object sender, EventArgs e)
{
i = 1;
this.lvwMail.Items.Clear();
GetMail("false");
}
/// <summary>
/// “删除”click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnDelete_Click(object sender, EventArgs e)
{
if (this.lvwMail.SelectedItems.Count==0)
{
MessageBox.Show("没有选择任何一项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string mailID = this.lvwMail.SelectedItems[0].Tag.ToString();
//int mailID = (int)this.lvwMail.SelectedItems[0].Tag;
if (i==0)
{
string sql=string.Format("update Mails set isdelete='false' where id={0} ", mailID);
UpdateMails(sql);
}
else
{
DialogResult result = MessageBox.Show("确定要删除该邮件?", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result==DialogResult.Yes)
{
string sql = string.Format("delete from mails where id={0}", mailID);
UpdateMails(sql);
}
}
}
}
/// <summary>
/// 刷新邮件箱
/// </summary>
/// <param name="sql"></param>
private void UpdateMails(string sql)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=GreenMail;Integrated Security=True");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
this.lvwMail.SelectedItems[0].Remove();
conn.Close();
}
private void tsbtnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
---sql脚本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mails]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Mails](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Sender] [nvarchar](150) NOT NULL,
[Accpetor] [nvarchar](150) NOT NULL,
[Subject] [nvarchar](150) NOT NULL,
[MailContent] [nvarchar](500) NULL,
[Data] [datetime] NOT NULL,
[IsDelete] [bit] NOT NULL CONSTRAINT [DF_Mails_IsDelete] DEFAULT ((0)),
CONSTRAINT [PK_Mails] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[CK_Mails]') AND parent_object_id = OBJECT_ID(N'[dbo].[Mails]'))
ALTER TABLE [dbo].[Mails] WITH CHECK ADD CONSTRAINT [CK_Mails] CHECK (([Isdelete]=(1) OR [IsDelete]=(0)))