Windows Form -----内容(6)
FORM1
FORM2
添加在前面已经做过了,不再做过多的追叙。实现修改和删除的功能,主要用的知识点是:ListView:
ListView:
主要是用来显示的控件。
一、有五种显示的视图:大图标、小图标、平铺、列表、详细—— View属性*************
二、ListView的结结构:
1.ListView包括好多个ListViewItem,每个ListViewItem就是所显示的一项。
ListView1.Items --- ListViewItem项的集合
例如:
ListViewItem li = new ListViewItem(..);
ListView1.Items.Add(li);
2.ListViewItem可以认为它包括两部分,一部分是项本身的东西,另一部份是子项集合。
(1)结构
主项上可以加图片。子项不能加图片。
ListViewItem的子项是ListViewItem.ListViewSubItem类型。它是放在ListViewItem的SubItems集合中的。
(2)ListViewItem的属性
Text - 项的文本
Tag - 项的值 li.Tag = data.Code;
ImageIndex - 项所使用的图片索引。——需要事先给ListView设置SmallImageList和LargeImageList
li.ImageIndex = 0;
SubItems -- 子项的集合
Font,ForeColor,BackColor......
if (data.Sex==true)
{
li.ForeColor = Color.Blue;
}
else
{
li.ForeColor = Color.Red;
}
例如:
ListViewItem li = new ListViewItem(..);
li.SubItems.Add("文字");
ListView1.Items.Add(li);
三、属性:
View - 五种视图状态
SmallImageList,LargeImageList
FullRowSelect - 选择整行
GridLines - 是否显示格式
SelectedItems - 选中项的集合
Columns - 列的集合。
Items - 项的集合。
Groups- 组的集合
注意:
1.如果要使用Details视图,一定需要先添加列。
2.进行修改或删除的时候,一般需要把主键放在每个ListViewItem的Tag里。使用窗体的构造函数把主键传给目标窗体。
代码:
class DBconnection
{
public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=5587725";
}
class Info
{
private string _Code;
public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private bool _Sex;
public bool Sex
{
get { return _Sex; }
set { _Sex = value; }
}
private string _Nation;
public string Nation
{
get { return _Nation; }
set { _Nation = value; }
}
private DateTime _Birthday;
public DateTime Birthday
{
get { return _Birthday; }
set { _Birthday = value; }
}
}
class InfoDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public InfoDA()
{
_Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public void Insert(string code,string name,bool sex,string nation,DateTime birthday)
{
_Cmd.CommandText = "insert into info values(@code,@name,@sex,@nation,@birthday)";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@code",code);
_Cmd.Parameters.AddWithValue("@name", name);
_Cmd.Parameters.AddWithValue("@sex", sex);
_Cmd.Parameters.AddWithValue("@nation", nation);
_Cmd.Parameters.AddWithValue("@birthday", birthday);
try
{
_Conn.Open();
_Cmd.ExecuteNonQuery();
}
finally
{
_Conn.Close();
}
}
public Info Select(string code)
{
_Cmd.CommandText = "select * from info where Code=@code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@code",code);
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
if (_DR.Read())
{
Info data = new Info();
data.Code = _DR["Code"].ToString();
data.Name = _DR["Name"].ToString();
data.Nation = _DR["Nation"].ToString();
data.Sex = Convert.ToBoolean(_DR["Sex"]);
data.Birthday = Convert.ToDateTime(_DR["Birthday"]);
return data;
}
else
{
return null;
}
}
finally
{
_Conn.Close();
}
}
public List<Info> Select()
{
List<Info> list = new List<Info>();
_Cmd.CommandText = "select * from info";
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
Info data = new Info();
data.Code = _DR["Code"].ToString();
data.Name = _DR["Name"].ToString();
data.Nation = _DR["Nation"].ToString();
data.Sex = Convert.ToBoolean(_DR["Sex"]);
data.Birthday = Convert.ToDateTime(_DR["Birthday"]);
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
public void Update(string code,string name,bool sex,string nation,DateTime birthday)
{
_Cmd.CommandText = "update info set Name=@name,Sex=@sex,Nation=@nation,Birthday=@birthday where Code=@code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@code", code);
_Cmd.Parameters.AddWithValue("@name", name);
_Cmd.Parameters.AddWithValue("@sex", sex);
_Cmd.Parameters.AddWithValue("@nation", nation);
_Cmd.Parameters.AddWithValue("@birthday", birthday);
try
{
_Conn.Open();
_Cmd.ExecuteNonQuery();
}
finally
{
_Conn.Close();
}
}
public void Delete(string code)
{
_Cmd.CommandText = "delete from info where Code=@code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@code", code);
try
{
_Conn.Open();
_Cmd.ExecuteNonQuery();
}
finally
{
_Conn.Close();
}
}
}
class Nation
{
//构造函数
public Nation(string code, string name)
{
_Name = name;
_Code = code;
}
private string _Code;
public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public override string ToString()
{
return Name;
}
}
class NationDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public NationDA()
{
_Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<Nation> Select()
{
List<Nation> list = new List<Nation>();
_Cmd.CommandText = "select * from Nation";
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
Nation data = new Nation(_DR["Code"].ToString(),_DR["Name"].ToString());
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
public Nation Select(string code)
{
_Cmd.CommandText = "select * from Nation where Code=@code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@code",code);
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
if (_DR.Read())
{
Nation data = new Nation(_DR["Code"].ToString(), _DR["Name"].ToString());
return data;
}
else
{
return null;
}
}
finally
{
_Conn.Close();
}
}
}
FORM1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void SHOWT()
{
listView1.Items.Clear();
//取数据
List<Info> list = new InfoDA().Select();
foreach (Info data in list)
{
//加进去
ListViewItem li = new ListViewItem(data.Name);
//加图片
li.ImageIndex = 0;
//给Tag 赋值
li.Tag = data.Code;
//颜色
if (data.Sex==true)
{
li.ForeColor = Color.Blue;
}
else
{
li.ForeColor = Color.Red;
}
li.SubItems.Add(data.Sex?"男":"女");
li.SubItems.Add(new NationDA().Select(data.Nation).Name);
li.SubItems.Add(data.Birthday.ToString("yyyy年MM月dd日"));
//分组
if (data.Sex==true)
{
li.Group = listView1.Groups[0];
}
else
{
li.Group = listView1.Groups[1];
}
listView1.Items.Add(li);
}
}
private void Form1_Load(object sender, EventArgs e)
{
SHOWT();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex==0)
{
listView1.View = View.LargeIcon;
}
else if (comboBox1.SelectedIndex==1)
{
listView1.View = View.SmallIcon;
}
else if (comboBox1.SelectedIndex==2)
{
listView1.View = View.Tile;
}
else if (comboBox1.SelectedIndex==3)
{
listView1.View = View.List;
}
else if (comboBox1.SelectedIndex==4)
{
listView1.View = View.Details;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
this.Text = listView1.SelectedItems[0].Tag.ToString();
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string key = listView1.SelectedItems[0].Tag.ToString();
Form2 f2 = new Form2(key);
f2.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string key = listView1.SelectedItems[0].Tag.ToString();
Form2 f2 = new Form2(key);
f2.Show();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string key = listView1.SelectedItems[0].Tag.ToString();
Form2 f2 = new Form2(key,this);
f2.Show();
}
}
private void button3_Click(object sender, EventArgs e)
{
//删除前确认弹窗
DialogResult result=MessageBox.Show("确认删除吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (result==System.Windows.Forms.DialogResult.Yes)
{
if (listView1.SelectedItems.Count > 0)
{
//取主键值
string key = listView1.SelectedItems[0].Tag.ToString();
//调用数据访问类,操作数据库()
//1.删工作简历(需要构造work 实体类,数据访问类,在此时间有限不写了)
//2.删家庭关系(需要构造family 实体类,数据访问类,在此时间有限不写了)
//3.删人员
InfoDA data = new InfoDA();
data.Delete(key);
//重新加载listview1
SHOWT();
}
}
}
}
FORM2:
public partial class Form2 : Form
{
private string _key = "";
private Form1 _Parent;
public Form2()
{
InitializeComponent();
}
public Form2(string key)
:this()
{
_key = key;
}
public Form2(string key,Form1 frm)
: this()
{
_key = key;
_Parent = frm;
}
//加载民族
private void Fillnation()
{
//找数据
List<Nation> list = new NationDA().Select();
//显示数据
cbmnation.DataSource = list;
cbmnation.DisplayMember = "Name";
cbmnation.ValueMember = "Code";
}
//加载人员信息
private void Fillinfo()
{
//取数据
Info data = new InfoDA().Select(_key);
//放进去
if (data != null)
{
textcode.Text = data.Code;
textname.Text = data.Name;
rbnboy.Checked = data.Sex;
rbngirl.Checked = !data.Sex;
dtpbirthday.Value = data.Birthday;
foreach (Nation nation in cbmnation.Items)
{
if (nation.Code == data.Nation)
{
cbmnation.SelectedItem = nation;
break;
}
}
}
}
private void Form2_Load(object sender, EventArgs e)
{
this.Text = _key;
Fillnation();
Fillinfo();
}
private void btncancle_Click(object sender, EventArgs e)
{
Close();
}
//更新回数据库
private void btnupdate_Click(object sender, EventArgs e)
{
//把界面上的值取出来
string code = textcode.Text;
string name = textname.Text;
bool sex = rbnboy.Checked;
string nation = (cbmnation.SelectedItem as Nation).Code;
DateTime birthday = dtpbirthday.Value;
//送回到数据库
InfoDA data = new InfoDA();
data.Update(code, name, sex, nation, birthday);
//刷新关闭
_Parent.SHOWT();
Close();
}
}