1.首先是添加员工信息:
public int type; //保存父窗体的引用 public FrmMain FrmParent { get; set; } // public FrmAdd() { InitializeComponent(); this.cboSex.SelectedIndex = 0; }
2.写在保存按钮里去: try { //创建SE的对象 SE se = new SE(); se.ID = this.txtid.Text.Trim(); se.Age = Int32.Parse(this.txtAge.Text.Trim()); se.Name = this.txtName.Text.Trim();//姓名 if (this.cboSex.SelectedItem.ToString() == "男") { se.Gender = Gender.男.ToString(); } else { se.Gender = Gender.女.ToString(); } //循环打印 foreach (SE item in FrmParent.AddList) { if (item.ID == se.ID) { MessageBox.Show("此工号已经存在!"); return; } } //添加泛型集合里 FrmParent.AddList.Add(se); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //刷新列表 this.FrmParent.BindGrid(FrmParent.AddList); } 理解: 主窗口
public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } public Dictionarydic = new Dictionary (); //泛型集合 public List AddList = new List (); //刷新DataGridView数据 public void BindGrid(List list) { this.divList.DataSource = new BindingList (list); } //主窗口 private void FrmMain_Load(object sender, EventArgs e) { } //新增 private void toolStripButton1_Click(object sender, EventArgs e) { FrmAdd fm = new FrmAdd(); fm.type = 1; fm.FrmParent = this; fm.ShowDialog(); } //查看 private void btnLook_Click(object sender, EventArgs e) { List tempList = new List ();//用于临时列表保存查询到的信息 foreach (SE item in this.AddList) { //如果泛型集合的SE下标不等于-1 if (item.ID.IndexOf(this.txtId.Text.Trim()) !=-1) { tempList.Add(item); } } this.divList.DataSource = new BindingList (tempList); } //删除 private void toolStripButton3_Click(object sender, EventArgs e) { DialogResult = MessageBox.Show("是否确定删除?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if (DialogResult ==DialogResult.Yes) { //通过索引访问 List tempList = new List (); foreach (SE item in this.AddList) { tempList.Remove(item); } this.divList.DataSource = new BindingList (tempList); } } //修改 private void toolStripButton2_Click(object sender, EventArgs e) { } private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) { //判断是否选中一行 if (this.divList.SelectedRows.Count != 1) { MessageBox.Show("请选中一行!"); return; } string workNo = divList.CurrentRow.Cells[0].Value.ToString(); //遍历Key值 foreach (string item in dic.Keys) { if (workNo == item) { MessageBox.Show("您已经签到过!"); return; } } Record record = new Record(); record.ID = workNo;//id号 record.Name = divList.CurrentRow.Cells[1].Value.ToString();//获取选中的姓名 record.SignInTime = DateTime.Now;//当前的时间 this.dic.Add(record.ID,record);//添加到记录里 MessageBox.Show("签到成功!"); } private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.divList.SelectedRows.Count !=1) { MessageBox.Show("请选择一行!"); return; } string ID = divList.CurrentRow.Cells[0].Value.ToString(); bool isOut = false;//标识是否已经签到过 foreach (string item in dic.Keys) { if (item == ID) { this.dic[item].SignOutTime = DateTime.Now; MessageBox.Show("签退成功!"); isOut = true; break; } } if (!isOut) { MessageBox.Show("很抱歉,尚未签到!"); } } private void toolStripButton4_Click(object sender, EventArgs e) { Frmclock fm = new Frmclock(); fm.FrmParent = this; fm.ShowDialog(); } }
public Frmclock() { InitializeComponent(); } //保存父级 public FrmMain FrmParent { get; set; } public void show() { BindingSource bs = new BindingSource(); bs.DataSource = FrmParent.dic.Values; this.dataGridView1.DataSource = bs; } private void Frmclock_Load(object sender, EventArgs e) { show(); }