使用集合组织相关数据
员工打卡记录项目开发
实现员工信息管理(新增、查询、删除)
实现员工考勤管理(签到、签退)
public class Record
{
//签到时间
public DateTime SignInTime { get; set; }
//签退时间
public DateTime SignOutTime { get; set; }
//工号
public string ID { get; set; }
//员工姓名
public string Name { get; set; }
}
public class SE
{
public string ID { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public string Gender { get; set; }
/// <summary>
/// 人气值
/// </summary>
private int _popularity = 0;
public int Popularity
{
get { return _popularity; }
set { _popularity = value; }
}
主窗口代码实现功能
public FrmMain()
{
InitializeComponent();
}
//列表,保存程序员对象
public List<SE> list = new List<SE>();
public Dictionary<string, Record> recordList = new Dictionary<string, Record>();
private void FrmMain_Load(object sender, EventArgs e)
{
Initial();
Binder(list);
}
/// <summary>
/// 初始信息
/// </summary>
private void Initial()
{
SE dd = new SE();
dd.ID = "001";
dd.Name = "李小龙";
dd.Age = 25;
dd.Gender = "男";
list.Add(dd);
//随便
SE two = new SE();
two.ID = "002";
two.Name = "王红";
two.Age = 18;
two.Gender = "女";
list.Add(two);
SE three = new SE();
three.ID = "003";
three.Name = "黎明";
three.Age = 30;
three.Gender = "男";
list.Add(three);
}
/// <summary>
/// 绑定数据
/// </summary>
/// <param name="list"></param>
public void Binder(List<SE> list)
{
dgvProgrammer.DataSource = new BindingList<SE>(list);
}
/// <summary>
/// 签到操作
/// </summary>
private void tsmiSignIn_Click(object sender, EventArgs e)
{
if (this.dgvProgrammer.SelectedRows.Count != 1)
{
MessageBox.Show("请选中一行!");
return;
}
string workNo = dgvProgrammer.CurrentRow.Cells[0].Value.ToString();
MessageBox.Show("签到成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
foreach (string id in recordList.Keys)
{
if (workNo == id)
{
MessageBox.Show("您已经签到过!");
}
}
Record record = new Record();
record.ID = workNo;
record.Name = dgvProgrammer.CurrentRow.Cells[1].Value.ToString();
record.SignInTime = DateTime.Now;
this.recordList.Add(record.ID, record);
}
/// <summary>
/// 签退操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiSignOut_Click(object sender, EventArgs e)
{
if (this.dgvProgrammer.SelectedRows.Count!=1)
{
MessageBox.Show("请选中一行!");
return;
}
string Id = dgvProgrammer.CurrentRow.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)
{
MessageBox.Show("很抱歉,尚未签到");
}
}
/// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsAdd_Click(object sender, EventArgs e)
{
FrmMaintance tance = new FrmMaintance();
tance.MaintanceType = 1;//为1执行添加
tance.frmParent = this;
tance.ShowDialog();
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsModi_Click(object sender, EventArgs e)
{
FrmMaintance frm = new FrmMaintance();
frm.Grid = this.dgvProgrammer.SelectedRows[0];
frm.MaintanceType = 0;//记录为0 执行修改
frm.frmParent = this;
frm.ShowDialog();
}
/// <summary>
/// 删除操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsDel_Click(object sender, EventArgs e)
{
//判断是否选中一行
if (this.dgvProgrammer.SelectedRows.Count != 1)
{
MessageBox.Show("请选中一行");
return;
}
//确认删除操作
DialogResult dr = MessageBox.Show("确实要删除这个员工的信息吗?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr != DialogResult.OK)
{
return;
}
//获取当前选中行的ID
string id = dgvProgrammer.CurrentRow.Cells["workNo"].Value.ToString();
//查找ID号对象并从集合中删除
foreach (SE item in list)
{
if (item.ID == id)
{
list.Remove(item);
break;
}
}
//刷新DataGridView显示
Binder(list);
MessageBox.Show("删除成功!", "执行信息");
}
/// <summary>
/// 跳转到打卡记录窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsRecord_Click(object sender, EventArgs e)
{
FrmRecord frm = new FrmRecord();
frm.recordList = this.recordList;
frm.ShowDialog();
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuery_Click(object sender, EventArgs e)
{
List<SE> temp = new List<SE>();
foreach (SE item in list)
{
if (item.ID.IndexOf(txtID.Text.Trim()) != -1)
{
temp.Add(item);
}
this.dgvProgrammer.DataSource = new BindingList<SE>(temp);
}
}
}
public partial class FrmRecord : Form
{
public FrmRecord()
{
InitializeComponent();
}
public Dictionary<string, Record> recordList { get; set; }
private void FrmRecord_Load(object sender, EventArgs e)
{
BindRecords();
}
private void BindRecords()
{
lblTitle.Text = string.Format("共有{0}条打卡记录", recordList.Count);
//将Dictionary<K,V>绑定到DataGridView控件
BindingSource bs = new BindingSource();
bs.DataSource = recordList.Values;
dgvRecords.DataSource = bs;
}
}