您的位置:首页 > 移动开发 > 微信开发

员工考勤信息管理小程序

2016-02-23 14:26 330 查看
虽然这是个小程序,但是呢还是用到了许多的知识点的.主要是""使用集合组织相关数据."",这个知识点非常重要.

在以后搞大型的项目,绝对离不开"集合组织数据".例如:ArrayList动态存储数据,HashTable的数据结构(哈希表).

泛型集合:List<T>和Dictionary<K,V>

泛型类.

下面呢就是一个"员工信息管理"小程序.用来强化知识点.

首先,创几个类:

SE类

public  class SE
{
public string ID { get; set; }
public int Age { get; set; }
public Sex Gender { get; set; }
public string Name { get; set; }
}


Gen类(枚举)

public  class Gen
{
//   public static 实现考勤信息管理.Gender 男 { get; set; }
}

public enum Sex
{

男, 女
}


Record类

{
public  class Record
{
//签到时间
public DateTime SingInTime { get; set; }
//签退时间
public DateTime SingOutTime { get; set; }
//工号
public string ID { get; set; }
//员工姓名
public string Name { get; set; }


管理信息:

添加信息:



代码段:

public int MaintanceType { get; set; }
//保存父窗体的引用
public FrmMain FrmParent { get; set; }
//初始化

//保存按钮的响应
private void btnOk_Click(object sender, EventArgs e)
{
try
{
SE pr = new SE();
pr.ID = this.txtID1.Text.Trim();//工号
pr.Age = Int32.Parse(this.txtAge1.Text.Trim());//年龄

if (this.cmbgender1.SelectedIndex.ToString() == "男")//性别
{
pr.Gender = Sex.男;
//这个也可以:pr.Gender=(Sex)(Enum.Parse(typeof(Sex),"男"));
}
else
{
pr.Gender = Sex.女;
}
pr.Name = this.txtName1.Text.Trim();//名字
//添加操作
//工号唯一性验证
if (this.MaintanceType == 1)
{
foreach (SE item in FrmParent.programmerList)
{
if (item.ID == pr.ID)
{
MessageBox.Show("此工号已存在");
return;

}
}
FrmParent.programmerList.Add(pr);
}
this.Close();
}
catch (Exception)
{
MessageBox.Show("出错啦");

}
finally
{

this.FrmParent.BindGrid(FrmParent.programmerList);
}


看看主界面:



主界面的主要代码

查询信息:



//查询信息
private void btnLook_Click(object sender, EventArgs e)
{
//根据员工号进行模糊查询
List<SE> teapList = new List<SE>();//用临时列表保存查询到的信息
foreach (SE item in this.programmerList)
{
if (item.ID.IndexOf(this.txtID.Text.Trim()) != -1)//indexof()实现模糊查询
{
teapList.Add(item);

}

}
this.dgvlist.DataSource = new BindingList<SE>(teapList);

}


删除信息:

private void toolStripButton3_Click(object sender, EventArgs e)
{
if (this.dgvlist.SelectedRows.Count == 0)
{
MessageBox.Show("你还未选中要删除的信息,请选择!");
return;

}
DialogResult result = MessageBox.Show("确认要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (DialogResult.OK != result)
{
return;
}
string sid = dgvlist.CurrentRow.Cells["ID"].Value.ToString();
foreach (SE item in programmerList)
{
if (item.ID == sid)
{
programmerList.Remove(item);
break;

}
}
//刷新
BindGrid(programmerList);
MessageBox.Show("删除成功!");
}


签到:



代码段:

//签到菜单项,
private void 签到ToolStripMenuItem_Click(object sender, EventArgs e)
{
//验证
//确保有选中的行
if (this.dgvlist.SelectedRows.Count != 1)
{
MessageBox.Show("请选择要签到的人");
return;
}
//确保没有签到过
string id = dgvlist.CurrentRow.Cells["ID"].Value.ToString();
foreach (string item in recordList.Keys)
{
if (id == item)
{
MessageBox.Show("您以前到过");
return;
}

}
//执行签到
Record record = new Record();
record.ID = id;
record.Name = dgvlist.CurrentRow.Cells["Name"].Value.ToString();
record.SingInTime = DateTime.Now;//获取当前系统时间
this.recordList.Add(record.ID, record);//添加到记录中
MessageBox.Show("签到成功!!");

}


签退:

//签退操作
private void 签退ToolStripMenuItem_Click(object sender, EventArgs e)
{
//确保有选中行
if (this.dgvlist.SelectedRows.Count != 1)
{
MessageBox.Show("请选择!");
return;
}
string id = dgvlist.CurrentRow.Cells["ID"].Value.ToString();
bool isOut = false;//标识是否已签过到
foreach (string item in recordList.Keys)
{
if (item == id)
{
//执行签到,设置签退时间
this.recordList[item].SingOutTime = DateTime.Now;
MessageBox.Show("签退成功!");
isOut = true;
break;
}
}
if (!isOut)
{
MessageBox.Show("很抱歉,尚未签到!");
}
}


打卡记录:



//打卡记录
public Dictionary<string, Record> recordList { get; set; }
//数据绑定
private void BindRecords()
{
lblcount.Text = "共有"+recordList.Count+"条打卡记录";
//将Dictionary<K,V>绑定到DataGridView控件

BindingSource bs = new BindingSource();
#region 绑定数据源 BindingSource
/*
*    封装窗体的数据源。
public BindingSource();
//
// 摘要:
//     初始化 System.Windows.Forms.BindingSource 类的新实例,并将 System.Windows.Forms.BindingSource
//     添加到指定的容器。
//
// 参数:
//   container:
//     要将当前 System.Windows.Forms.BindingSource 添加到的 System.ComponentModel.IContainer。

*
public BindingSource(IContainer container);
//
// 摘要:
//     用指定的数据源和数据成员初始化 System.Windows.Forms.BindingSource 类的新实例。
//
// 参数:
//   dataSource:
//     System.Windows.Forms.BindingSource 的数据源。
//
//   dataMember:
//     要绑定到的数据源中的特定列或列表名称。
public BindingSource(object dataSource, string dataMember);

// 摘要:
//     获取一个值,该值指示是否可以编辑基础列表中的项。
//
// 返回结果:
//     true 指示列表项可以编辑;否则为 false。

*/
#endregion

bs.DataSource = recordList.Values;
dgvlist.DataSource = bs;

}

private void FrmRecord_Load(object sender, EventArgs e)
{
BindRecords();
}


这样最后呢,就可以很好的掌握了这些知识点------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: