雖然這是個小程序,但是呢還是用到了許多的知識點的.主要是""使用集合組織相關數據."",這個知識點非常重要.
在以後搞大型的項目,絕對離不開"集合組織數據".例如: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();
}
這樣最後呢,就可以完完整整的掌握了這些知識點------------------------------------