你的位置:首页 > ASP.net教程

[ASP.net教程]用WinForm写的员工考勤项目!!!!!!


先说几句,作为一个还在学习的程序员,掌握的知识有限;但我利用自身所学,给一些像我一样还在学习的码农提供我的绵薄之力!

写的不好,但是尽力了,希望大牛指点。多多吐槽!!!

   好了开始说项目需求: 实现新增员工;使用DataGridView控件展示员工信息;实现根据员工工号进行模糊查询;实现删除员工信息;每天只能签到一次;签退前必须已经签到

看到这些需求,我们先要思考下该做什么,理清思路才能更快的完成项目。我通常先搭建窗体,然后根据要求逐步实现项目要求!

这是我们所要用的到的三个窗体

我们建一个辅助类,初始化一个单列的泛型集合,给单列的泛型集合付个初值

 

class Worker  {    private string id;    private string name;    private int age;    private string sex;    public string ID    {      get      {        return id;      }      set      {        id = value;      }    }    public string Name    {      get      {        return name;      }      set      {        name = value;      }    }    public int Age    {      get      {        return age;      }      set      {        age = value;      }    }    public string Sex    {      get      {        return sex;      }      set      {        sex = value;      }    }        public static List<Worker> list = new List<Worker>();    public static void Insert()    {      Worker w1 = new Worker();      w1.ID = "20090101";      w1.Name = "王小毛";      w1.Age = 20;      w1.Sex = "男";      list.Add(w1);      Worker w2 = new Worker();      w2.ID = "20090102";      w2.Name = "周新雨";      w2.Age = 19;      w2.Sex = "女";      list.Add(w2);    }  }

 这是员工信息维护窗体的Load事件的代码

 private void FrmSecond_Load(object sender, EventArgs e)    {
如果list没有数据就添加数据,确保调用这个窗体是,只有在没有数据时才能添加数据! if (Worker.list.Count==0) { Worker.Insert(); }
清除datagridview的英文列,并且绑定数据 dgvList.AutoGenerateColumns = false; dgvList.DataSource = Worker.list; }

 这是新增里的代码

private void toolStripButton1_Click(object sender, EventArgs e)    {
调用添加员工员工信息窗体 FrmFirst ff = new FrmFirst(); this.Hide(); ff.Show(); }

private void btn1_Click(object sender, EventArgs e)    {
确保一个工号只能添加一次 foreach (Worker item in Worker.list) { if (item.ID==txt01.Text) { MessageBox.Show("工号已存在!!!!"); return; } }
添加一个员工信息 Worker w1 = new Worker(); w1.ID = txt01.Text; w1.Name = txt03.Text; w1.Age = Convert.ToInt32(txt02.Text); w1.Sex =cob01.Text; Worker.list.Add(w1); FrmSecond fs = new FrmSecond(); this.Close(); fs.Show(); }

 

这是删除的代码
private void toolStripButton3_Click(object sender, EventArgs e) {
提示点击是才会删除 DialogResult result = MessageBox.Show("提示","是否删除",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if(result==DialogResult.Yes) {
string id = dgvList.SelectedRows[0].Cells[0].Value.ToString(); //string id = dgvList.SelectedCells[0].Value.ToString();
如果ID和我选中的ID相等才会删除 foreach (Worker item in Worker.list) { if(item.ID==id) { Worker.list.Remove(item); break; } } } //dgvList.AutoGenerateColumns = false; dgvList.DataSource = new BindingList<Worker>(Worker.list); }

 

这段是重点难点在写这段之前先建好另一个辅助类
  class Record
    {
        public string id { get; set; }
        public string name { get; set; }
        public DateTime SignInTime { get; set; }
        public DateTime SignOutTime { get; set; }
        创建一个双列泛型集合
        public static Dictionary<string, Record> relist = new Dictionary<string, Record>();
      
    }

public int num; private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) {
给num赋值,把你选中的type值付给num Worker w = new Worker(); foreach (Worker item in Worker.list) { if (item.Name == dgvList.SelectedCells[1].Value.ToString()) { w = item; break; } } num = w.type;
判断如果num是0才可以签到 if (num == 0) { Record r = new Record(); r.id = dgvList.SelectedRows[0].Cells[0].Value.ToString(); r.name = dgvList.SelectedRows[0].Cells[1].Value.ToString(); r.SignInTime = DateTime.Now; Record.relist.Add(r.name, r);
给你选中的type赋值一个1 w.type = 1; MessageBox.Show("签到成功"); } else { MessageBox.Show("您已经签到过了"); } } private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) { Worker w = new Worker(); foreach (Worker item in Worker.list) { if (item.Name == dgvList.SelectedCells[1].Value.ToString()) { w = item; break; } }
把你选中的type赋值给num num = w.type;
如果num是1,才可以签退 if (num == 1) { foreach (string item in Record.relist.Keys) { if (item == dgvList.SelectedRows[0].Cells[1].Value.ToString()) { Record.relist[dgvList.SelectedRows[0].Cells[1].Value.ToString()].SignOutTime = DateTime.Now; w.type = 2; MessageBox.Show("签退成功"); break; } } }
如果是0证明没有签到 else if (num == 0) { MessageBox.Show("没有签到,无法签退"); }
否则已经签退过了 else { MessageBox.Show("您已经签退过了"); } //dgvList.AutoGenerateColumns = false; //dgvList.DataSource = new BindingList<Worker>(Worker.list); }

  


把员工的签到信息展示在DataGridView上

private void FrmThisd_Load(object sender, EventArgs e) { dgvList.AutoGenerateColumns = false; BindingSource bs = new BindingSource(); bs.DataSource = Record.relist.Values; dgvList.DataSource = bs; lbl1.Text = dgvList.RowCount.ToString(); }