一、首先实现主界面员工信息维护。
1.关键代码,用list绑定datagridviewDatagridview 对象.Datasoure=list 对象;2.当list数据经过增删改,需要转正datagridview.datasoure=new bindinglist<T>(mylist)3.保存父窗体的引用public frmmain frmparent{get;set;}(一)在父窗体load事件里赋值public Listss = new List (); private void Frmmian_Load(object sender, EventArgs e) { //实例化对象,给他们初始值 se x = new se(); x.Id = 1; x.Name = "店小一"; x.Age = 3; x.sex = "男"; se x1 = new se(); x1.Id = 2; x1.Name = "大运"; x1.Age = 23; x1.sex = "男"; ss.Add(x); ss.Add(x1); //绑定dataGridView数据 dataGridView1.DataSource = new BindingList (ss); }
二、增加员工
关键代码: this.frmparent.bindgrid(frmparent.ss); //刷新父窗体 // bindgrid方法在父窗体://public void bindgrid(Listss) { //刷新datagridview数据 this.dataGridView1.DataSource = new BindingList (ss); }//重新new员工类se,当前窗体控件里对应的值分别传到新建的员工类里。加if判断是男是女,根据当前新new的工号循坏判断总的是否有相同的,如果有则提示相同 try { se pr = new se(); pr.Id = Convert.ToInt32(this.txtid.Text.Trim()); pr.Age = Int32.Parse(this.txtage.Text.Trim()); pr.Name = this.txtname.Text.Trim(); if (this.txtsex.SelectedItem.ToString() == "男") { pr.sex = "男"; } else { pr.sex = "女"; } foreach (se item in frmparent.ss) { //员工类 父窗体里的数据 if (item.Id == pr.Id) { MessageBox.Show("此工号已存在"); return; } } frmparent.ss.Add(pr); this.Close(); } catch (Exception ex) { MessageBox.Show("出错!" + ex.Message); } finally { this.frmparent.bindgrid(frmparent.ss); //刷新父窗体代码 }
三、实现删除功能
//获取当前选中的,通过id删除private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.dataGridView1.SelectedRows.Count > 0) { DialogResult choice = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel); if (choice == DialogResult.OK) { string id = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//拿到选中的员工号进行删除 for (int i = 0; i < ss.Count; i++) { if (ss[i].Id.ToString() == id) { //执行删除操作 ss.Remove(ss[i]); //刷新DataGridView bindgrid(ss); MessageBox.Show("删除成功!"); } } } else { MessageBox.Show("请选择一行!"); return;
四、签到签退
首先定义record类,里面保存签退签到时间,工号以及员工姓名主窗体里,先判断是否选中,确保是否签到过,如果签到,获取新的名字以及时间private Dictionaryrec = new Dictionary (); private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) { //判断是否选中一行 if (this.dataGridView1.SelectedRows.Count != 1) { MessageBox.Show("请选中一行!"); return; } string workNo = dataGridView1.CurrentRow.Cells[0].Value.ToString(); //遍历Key值 foreach (string item in rec.Keys) { if (workNo == item) { MessageBox.Show("您已经签到过!"); return; } } Record record = new Record(); record.Id = workNo;//id号 record.Name = dataGridView1.CurrentRow.Cells[1].Value.ToString();//获取选中的姓名 record.Intime = DateTime.Now;//当前的时间 this.rec.Add(record.Id, record);//添加到记录里 MessageBox.Show("签到成功!"); }签退: if (this.dataGridView1.SelectedRows.Count != 1) { MessageBox.Show("请选择一行!"); return; } string ID = dataGridView1.CurrentRow.Cells[0].Value.ToString(); bool isOut = false;//标识是否已经签到过 foreach (string item in rec.Keys) { if (item == ID) { this.rec[item].Outtime = DateTime.Now; MessageBox.Show("签退成功!"); isOut = true; break; } } if (!isOut) { MessageBox.Show("很抱歉,尚未签到!"); }
最后绑定数据:
public Frmmian frmparent { get; set; } public void show { BindingSource bs = new BindingSource(); bs.DataSource = FrmParent.dic.Values; this.dataGridView1.DataSource = bs; } } }