FrmMain界面
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Day03_0200员工打卡 12 { 13 public partial class FrmMain : Form 14 { 15 public FrmMain() 16 { 17 InitializeComponent(); 18 } 19 //列表,用于保存SE对象 20 public List<SE> programmerList = new List<SE>(); 21 //记录打卡记录 22 public Dictionary<string, Record> recordList = new Dictionary<string, Record>(); 23 //刷新DataGridView数据 24 public void BindGrid(List<SE> list) 25 { 26 this.dataGridView1.DataSource = new BindingList<SE>(list); 27 } 28 29 private void toolStripLabel1_Click(object sender, EventArgs e) 30 { 31 FrmMaintance frm = new FrmMaintance(); 32 frm.FrmParent = this; 33 frm.ShowDialog(); 34 35 } 36 37 private void btnSelect_Click(object sender, EventArgs e) 38 { 39 List<SE> tempList = new List<SE>(); //用临时列表保存查询到的信息 40 foreach (SE item in this.programmerList) 41 { 42 if (item.Id==txtId.Text || item.Name.Equals(txtName.Text)) 43 { 44 tempList.Add(item); 45 } 46 } 47 this.dataGridView1.DataSource = new BindingList<SE>(tempList); 48 } 49 50 private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) 51 { 52 //验证 53 //确保有选中的行 54 if (this.dataGridView1.SelectedRows.Count !=1) 55 { 56 MessageBox.Show("请选中一行"); 57 return; 58 } 59 //确保没有签过到 60 string workNo = dataGridView1.CurrentRow.Cells["Id"].Value.ToString(); 61 foreach (string id in recordList.Keys) 62 { 63 if (workNo==id) 64 { 65 MessageBox.Show("您已经签过到!"); 66 return; 67 } 68 } 69 //执行签到 70 Record record = new Record(); 71 record.Id = workNo; 72 record.Name = dataGridView1.CurrentRow.Cells["names"].Value.ToString(); 73 record.SignInTime = DateTime.Now; //获取系统当前时间 74 this.recordList.Add(record.Id, record); //添加到签到记录中 75 MessageBox.Show("签到成功!"); 76 77 } 78 //签退操作 79 private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) 80 { 81 //确保有选中的行 82 if (this.dataGridView1.SelectedRows.Count != 1) 83 { 84 MessageBox.Show("请选中一行"); 85 return; 86 } 87 string ID = dataGridView1.CurrentRow.Cells["Id"].Value.ToString(); 88 bool isOut = false; //标识是否已经签到 89 foreach (string key in recordList.Keys) 90 { 91 if(key==ID) 92 { 93 //执行签到,设置签退时间 94 this.recordList[key].SignOutTime = DateTime.Now; 95 MessageBox.Show("签退成功"); 96 isOut = true; 97 break; 98 } 99 } 100 if(!isOut) 101 { 102 MessageBox.Show("很抱歉,尚未签到!"); 103 } 104 } 105 106 private void toolStripLabel4_Click(object sender, EventArgs e) 107 { 108 FrmRecord fd = new FrmRecord(); 109 fd.fm = this; 110 fd.Show(); 111 } 112 113 private void toolStripLabel3_Click(object sender, EventArgs e) 114 { 115 if (this.dataGridView1.SelectedRows.Count !=1) 116 { 117 MessageBox.Show("请选中一行"); 118 return; 119 } 120 int i = dataGridView1.CurrentCell.RowIndex; 121 programmerList.RemoveAt(i); 122 BindGrid(programmerList); 123 MessageBox.Show("删除成功!"); 124 } 125 126 private void FrmMain_Load(object sender, EventArgs e) 127 { 128 SE pr1 = new SE(); 129 pr1.Id = "1001"; 130 pr1.Name = "张三"; 131 pr1.Sex = "男"; 132 pr1.Age = 5; 133 134 SE pr2 = new SE(); 135 pr2.Id = "1002"; 136 pr2.Name = "陈晨"; 137 pr2.Sex = "女"; 138 pr2.Age = 2; 139 140 SE pr3 = new SE(); 141 pr3.Id = "1003"; 142 pr3.Name = "王亮亮"; 143 pr3.Sex = "男"; 144 pr3.Age = 3; 145 146 programmerList.Add(pr1); 147 programmerList.Add(pr2); 148 programmerList.Add(pr3); 149 dataGridView1.DataSource = programmerList; 150 } 151 } 152 }
FrmMaintance界面
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Day03_0200员工打卡 12 { 13 public partial class FrmMaintance : Form 14 { 15 //保存父窗体的引用 16 public FrmMain FrmParent { get; set; } 17 public FrmMaintance() 18 { 19 InitializeComponent(); 20 this.cmbSex.SelectedIndex = 0; 21 } 22 23 private void btnSave_Click(object sender, EventArgs e) 24 { 25 26 try 27 { 28 //输入处理 29 SE se = new SE(); 30 se.Id = this.txtId.Text.Trim(); 31 se.Age = Int32.Parse(this.txtAge.Text.Trim()); //年龄 32 if (this.cmbSex.SelectedItem.ToString() == "男") 33 { 34 se.Sex = "男"; 35 } 36 else 37 { 38 se.Sex = "女"; 39 } 40 se.Name = this.txtName.Text.Trim(); //姓名 41 42 //添加操作 43 //学号唯一验证 44 foreach (SE item in FrmParent.programmerList) 45 { 46 if (item.Id == se.Id) 47 { 48 MessageBox.Show("此学号已存在"); 49 return; 50 } 51 } 52 FrmParent.programmerList.Add(se); 53 this.Close(); 54 } 55 catch (Exception ex) 56 { 57 MessageBox.Show("出错:" + ex.Message); 58 } 59 finally 60 { 61 //刷新父窗体的信息 62 this.FrmParent.BindGrid(FrmParent.programmerList); 63 } 64 } 65 66 private void btnNull_Click(object sender, EventArgs e) 67 { 68 txtId.Clear(); 69 txtName.Clear(); 70 txtAge.Clear(); 71 } 72 } 73 }
FrmRecord界面
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Day03_0200员工打卡 12 { 13 public partial class FrmRecord : Form 14 { 15 public FrmRecord() 16 { 17 InitializeComponent(); 18 } 19 public FrmMain fm{get;set;} 20 private void FrmRecord_Load(object sender, EventArgs e) 21 { 22 BindingSource ba = new BindingSource(); 23 ba.DataSource = fm.recordList.Values; 24 this.dataGridView1.DataSource = ba; 25 } 26 } 27 }