主窗体
using System:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myDgv
{
public partial class FrmMain : Form
{
//记录打卡记录的Dictionary
public Dictionary<string, Record> recordList = new Dictionary<string, Record>();
//用来存放临时表
List<SE> temp = new List<SE>();
public FrmMain()
{
InitializeComponent();
}
//列表,用于保存 se对象
public List<SE> programmerList = new List<SE>();
//刷新DataGridview 数据
public void BindGrid(List<SE> list)
{
this.dataGridView1.DataSource = new BindingList<SE>(list);
}
private void button1_Click(object sender, EventArgs e)
{
this.temp.Clear();
chaXun();
}
private void xianShi()
{
// List<SE> li = new List<SE>();
programmerList.Add(new SE() { ID = "111", Name = "大豆", Age = 12, Sex = "男" });
programmerList.Add(new SE() { ID = "222", Name = "黄豆", Age = 12, Sex = "男" });
programmerList.Add(new SE() { ID = "333", Name = "小毛豆", Age = 12, Sex = "女" });
dataGridView1.DataSource = new BindingList<SE>(programmerList);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
frmMaintance frm = new frmMaintance();
frm.frmParent = this;
frm.ShowDialog();
}
private void FrmMain_Load(object sender, EventArgs e)
{
xianShi();
}
/// <summary>
/// 根据员工工号进行模糊查询
/// </summary>
public void chaXun()
{
//用来存放临时表
// List<SE> temp = new List<SE>();
foreach (SE item in this.programmerList)
{
if (item.ID.IndexOf(this.textBox1.Text.Trim())!=-1)
{
temp.Add(item);
}
}
this.dataGridView1.DataSource = new BindingList<SE>(temp);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
DialogResult re = MessageBox.Show("确认要删除该数据吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (re == DialogResult.OK)
{
foreach (SE item in this.programmerList)
{
if (dataGridView1.SelectedRows[0].Cells[0].Value==item.ID)
{
programmerList.Remove(item);
break;
}
}
MessageBox.Show("删除成功");
this.dataGridView1.DataSource = new BindingList<SE>(programmerList);
}
//this.dataGridView1.DataSource = new BindingList<SE>(temp);
}
/// <summary>
///员工签到签退
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 签到ToolStripMenuItem_Click(object sender, EventArgs e)
{
//验证
//确保有选中的行
if (this.dataGridView1.SelectedRows.Count!=1)
{
MessageBox.Show("请选中一行");
return;
}
//确保没有签过到
// string workID = dataGridView1.CurrentRow.Cells["Column1"].Value.ToString();
string workID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
foreach (string id in recordList.Keys)
{
if (workID==id)
{
MessageBox.Show("您已经签过到了");
return;
}
}
//执行签到
Record re = new Record();
re.ID=workID;
// re.Name = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
re.Name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
re.SignInTime = DateTime.Now; //获取系统当前时间
this.recordList.Add(re.ID,re);
MessageBox.Show("签到成功");
}
//签退操作
private void 签退ToolStripMenuItem_Click(object sender, EventArgs e)
{
//确保有选中的行
if (this.dataGridView1.SelectedRows.Count !=1)
{
MessageBox.Show("请选中一行");
return;
}
// string ID = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
string ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
bool isOut = false; //标识是否已经签到过
foreach (string key in recordList.Keys)
{
if (key==ID)
{
//执行签退 设置签退时间
this.recordList[key].SignOutTime = DateTime.Now;
MessageBox.Show("签退成功");
isOut = true;
break;
}
}
if (isOut==false)
{
MessageBox.Show("很抱歉,尚未签到!");
}
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
frmRecord frm=new frmRecord();
frm.re = this.recordList;
frm.ShowDialog();
}
}
}
添加窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myDgv
{
public partial class frmMaintance : Form
{
//保存父窗体的应用
public FrmMain frmParent { get; set; }
public frmMaintance()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SE pr = new SE();
pr.ID = this.textBox1.Text.Trim();
pr.Name = this.textBox2.Text.Trim();
pr.Age = Int32.Parse(this.textBox3.Text.Trim());
pr.Sex = this.comboBox1.Text.Trim();
//工号唯一性验证
foreach (SE item in frmParent.programmerList)
{
if (item.ID == pr.ID)
{
MessageBox.Show("此工号以存在");
return;
}
}
frmParent.programmerList.Add(pr);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("出错" + ex.Message);
}
finally
{
//刷新父窗体
this.frmParent.BindGrid(frmParent.programmerList);
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void frmMaintance_Load(object sender, EventArgs e)
{
}
}
}
打卡记录窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myDgv
{
public partial class frmRecord : Form
{
//打卡记录
public Dictionary<string,Record> re { get; set; }
public frmRecord()
{
InitializeComponent();
}
private void frmRecord_Load(object sender, EventArgs e)
{
BindRecords();
}
public void BindRecords()
{
this.label1.Text = string.Format("共有{0}条打卡记录", this.re.Count);
BindingSource bs = new BindingSource();
bs.DataSource = re.Values;
this.dataGridView1.DataSource = bs;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
打卡记录类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myDgv
{
//打卡记录类
public class Record
{
//签到时间
public DateTime SignInTime { get; set; }
//签退时间
public DateTime SignOutTime { get; set; }
//工号
public string ID { get; set; }
//姓名
public string Name { get; set; }
}
}
员工类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myDgv
{
public class SE
{
public string ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}