先說幾句,作為一個還在學習的程式員,掌握的知識有限;但我利用自身所學,給一些像我一樣還在學習的碼農提供我的綿薄之力! 寫的不好,但是儘力了,希望大牛指點。多多吐槽!!! 好了開始說項目需求: 實現新增員工;使用DataGridView控制項展示員工信息;實現根據員工工號進行模糊查詢;實現刪除員工信息; ...
先說幾句,作為一個還在學習的程式員,掌握的知識有限;但我利用自身所學,給一些像我一樣還在學習的碼農提供我的綿薄之力!
寫的不好,但是儘力了,希望大牛指點。多多吐槽!!!
好了開始說項目需求: 實現新增員工;使用DataGridView控制項展示員工信息;實現根據員工工號進行模糊查詢;實現刪除員工信息;每天只能簽到一次;簽退前必須已經簽到
看到這些需求,我們先要思考下該做什麼,理清思路才能更快的完成項目。我通常先搭建窗體,然後根據要求逐步實現項目要求!
這是我們所要用的到的三個窗體
我們建一個輔助類,初始化一個單列的泛型集合,給單列的泛型集合付個初值
class Worker { private string id; private string name; private int age; private string sex; public string ID { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public string Sex { get { return sex; } set { sex = value; } } public static List<Worker> list = new List<Worker>(); public static void Insert() { Worker w1 = new Worker(); w1.ID = "20090101"; w1.Name = "王小毛"; w1.Age = 20; w1.Sex = "男"; list.Add(w1); Worker w2 = new Worker(); w2.ID = "20090102"; w2.Name = "周新雨"; w2.Age = 19; w2.Sex = "女"; list.Add(w2); } }
這是員工信息維護窗體的Load事件的代碼
private void FrmSecond_Load(object sender, EventArgs e) {
如果list沒有數據就添加數據,確保調用這個窗體是,只有在沒有數據時才能添加數據! if (Worker.list.Count==0) { Worker.Insert(); }
清除datagridview的英文列,並且綁定數據 dgvList.AutoGenerateColumns = false; dgvList.DataSource = Worker.list; }
這是新增里的代碼
private void toolStripButton1_Click(object sender, EventArgs e) {
調用添加員工員工信息窗體 FrmFirst ff = new FrmFirst(); this.Hide(); ff.Show(); }
private void btn1_Click(object sender, EventArgs e) {
確保一個工號只能添加一次 foreach (Worker item in Worker.list) { if (item.ID==txt01.Text) { MessageBox.Show("工號已存在!!!!"); return; } }
添加一個員工信息 Worker w1 = new Worker(); w1.ID = txt01.Text; w1.Name = txt03.Text; w1.Age = Convert.ToInt32(txt02.Text); w1.Sex =cob01.Text; Worker.list.Add(w1); FrmSecond fs = new FrmSecond(); this.Close(); fs.Show(); }
這是刪除的代碼
private void toolStripButton3_Click(object sender, EventArgs e) {
提示點擊是才會刪除 DialogResult result = MessageBox.Show("提示","是否刪除",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if(result==DialogResult.Yes) {
string id = dgvList.SelectedRows[0].Cells[0].Value.ToString(); //string id = dgvList.SelectedCells[0].Value.ToString();
如果ID和我選中的ID相等才會刪除 foreach (Worker item in Worker.list) { if(item.ID==id) { Worker.list.Remove(item); break; } } } //dgvList.AutoGenerateColumns = false; dgvList.DataSource = new BindingList<Worker>(Worker.list); }
這段是重點難點在寫這段之前先建好另一個輔助類
class Record
{
public string id { get; set; }
public string name { get; set; }
public DateTime SignInTime { get; set; }
public DateTime SignOutTime { get; set; }
創建一個雙列泛型集合
public static Dictionary<string, Record> relist = new Dictionary<string, Record>();
}
public int num; private void 簽到ToolStripMenuItem_Click(object sender, EventArgs e) {
給num賦值,把你選中的type值付給num Worker w = new Worker(); foreach (Worker item in Worker.list) { if (item.Name == dgvList.SelectedCells[1].Value.ToString()) { w = item; break; } } num = w.type;
判斷如果num是0才可以簽到 if (num == 0) { Record r = new Record(); r.id = dgvList.SelectedRows[0].Cells[0].Value.ToString(); r.name = dgvList.SelectedRows[0].Cells[1].Value.ToString(); r.SignInTime = DateTime.Now; Record.relist.Add(r.name, r);
給你選中的type賦值一個1 w.type = 1; MessageBox.Show("簽到成功"); } else { MessageBox.Show("您已經簽到過了"); } } private void 簽退ToolStripMenuItem_Click(object sender, EventArgs e) { Worker w = new Worker(); foreach (Worker item in Worker.list) { if (item.Name == dgvList.SelectedCells[1].Value.ToString()) { w = item; break; } }
把你選中的type賦值給num num = w.type;
如果num是1,才可以簽退 if (num == 1) { foreach (string item in Record.relist.Keys) { if (item == dgvList.SelectedRows[0].Cells[1].Value.ToString()) { Record.relist[dgvList.SelectedRows[0].Cells[1].Value.ToString()].SignOutTime = DateTime.Now; w.type = 2; MessageBox.Show("簽退成功"); break; } } }
如果是0證明沒有簽到 else if (num == 0) { MessageBox.Show("沒有簽到,無法簽退"); }
否則已經簽退過了 else { MessageBox.Show("您已經簽退過了"); } //dgvList.AutoGenerateColumns = false; //dgvList.DataSource = new BindingList<Worker>(Worker.list); }
把員工的簽到信息展示在DataGridView上
private void FrmThisd_Load(object sender, EventArgs e) { dgvList.AutoGenerateColumns = false; BindingSource bs = new BindingSource(); bs.DataSource = Record.relist.Values; dgvList.DataSource = bs; lbl1.Text = dgvList.RowCount.ToString(); }