public class SE
{
public string Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
public enum Gender
{
男,女
}
1 public class Record
2 {
3 //签到时间
4 public DateTime SignInTime { get; set; }
5 //签退时间
6 public DateTime SignOutTime { get; set; }
7 //工号
8 public string Id { get; set; }
9 //员工姓名
10 public string Name { get; set; }
11 }
![]()
![]()
1 private void frmKa_Load(object sender, EventArgs e)
2 {
3 dataGridView1.AutoGenerateColumns = false;
4 BindingSource bs = new BindingSource();
5 bs.DataSource = dic.Values;
6 this.dataGridView1.DataSource = bs;
7 }
1 //列表上用于保存SE对象
2 public List<SE> programmerList = new List<SE>();
3 //刷新DataGridView数据
4 public void BindGrid(List<SE> list)
5 {
6 this.dataGridView1.DataSource = new BindingList<SE>(list);
7 }
8
9 private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
10 {
11 frmMaintance frm = new frmMaintance();
12 // frm.MaintanceType = 1;
13 frm.FrmParent = this;
14 frm.ShowDialog();
15 }
16
17 private void btnCha_Click(object sender, EventArgs e)
18 {
19 //根据员工工号进行模糊查询
20 List<SE> list = new List<SE>();//用临时列表保存查询到的信息
21 foreach (SE item in this.programmerList)
22 {
23 if (item.Id.IndexOf(this.txtNo.Text.Trim()) != -1)
24 {
25 list.Add(item);
26 }
27 }
28 this.dataGridView1.DataSource = new BindingList<SE>(list);
29 }
30
31 private Dictionary<string, Record> recordList = new Dictionary<string, Record>();
32 private void 签到ToolStripMenuItem_Click(object sender, EventArgs e)
33 {
34 //确保有选中的行
35 if (this.dataGridView1.SelectedRows.Count != 1)
36 {
37 MessageBox.Show("请选中一行!");
38 return;
39 }
40 //确认是否签到
41 string workNo = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
42 foreach (string id in recordList.Keys)
43 {
44 if (workNo == id)
45 {
46 MessageBox.Show("您已经签过到!");
47 return;
48 }
49 }
50 //执行签到
51 Record record = new Record();
52 record.Id = workNo;
53 record.Name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
54 record.SignInTime = DateTime.Now;//获取系统当前时间
55 this.recordList.Add(record.Id, record);//添加到签到记录中
56 MessageBox.Show("签到成功!");
57 }
58
59 private void 签退ToolStripMenuItem_Click(object sender, EventArgs e)
60 {
61 //确保有选中的行
62 if (this.dataGridView1.SelectedRows.Count != 1)
63 {
64 MessageBox.Show("请选中一行!");
65 return;
66 }
67 string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
68 bool isOut = false;//标识是否已经签到过
69 foreach (string key in recordList.Keys)
70 {
71 if (key == ID)
72 {
73 this.recordList[key].SignOutTime = DateTime.Now;
74 MessageBox.Show("签退成功!");
75 isOut = true;
76 break;
77 }
78 }
79 if (!isOut)
80 {
81 MessageBox.Show("很抱歉,尚未签到!");
82 }
83 }
84 private void 打卡记录ToolStripMenuItem_Click(object sender, EventArgs e)
85 {
86 frmKa frm = new frmKa();
87 frm.dic = this.recordList;
88 frm.Show();
89
90
91
92 }
93
94 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
95 {
96 if(this.dataGridView1.SelectedRows.Count>0)
97 {
98 DialogResult ch=MessageBox.Show("确定要删除么?","提示",MessageBoxButtons.OKCancel);
99 if (ch == DialogResult.OK)
100 {
101 string id = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
102 for (int i = 0; i < list.Count; i++)
103 {
104 if (list[i].ID1 == id)
105 {
106 list.Remove(list[i]);
107 BinGrid(list);
108 MessageBox.Show("删除成功");
109 }
110 }
111
112 }
113 else
114 {
115 MessageBox.Show("请选择一行!");
116 return;
117 }
118
119 }
![]()
1 private void btnCun_Click(object sender, EventArgs e)
2 {
3 try
4 {
5 SE pr = new SE();
6 pr.Id = this.txtNo.Text.Trim();//工号
7 pr.Age = Int32.Parse(this.txtAge.Text.Trim());//年龄
8 pr.Name = this.txtName.Text.Trim();//姓名
9 if (this.cobGender.SelectedItem.ToString() == "男")//性别
10 {
11 pr.Gender = Gender.男.ToString();
12 }
13 else
14 {
15 pr.Gender = Gender.女.ToString();
16 }
17 //验证工号唯一性
18 foreach (SE item in FrmParent.programmerList)
19 {
20 if (item.Id == pr.Id)
21 {
22 MessageBox.Show("此工号已经存在!");
23 return;
24 }
25 }
26 FrmParent.programmerList.Add(pr);
27 this.Close();
28 }
29 catch (Exception ex)
30 {
31
32 MessageBox.Show("系统出现异常!" + ex);
33 }
34 finally
35 {
36 this.FrmParent.BindGrid(FrmParent.programmerList);
37 }
38 }