![](https://i-blog.csdnimg.cn/blog_migrate/0a404ba9fa19e622d3e908d8b9ce3282.png)
![](https://i-blog.csdnimg.cn/blog_migrate/22d772ae832a1cadf2d40c5bb7567e18.png)
![](https://i-blog.csdnimg.cn/blog_migrate/05ea150a056828c991c899b87230eefd.png)
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace GenericDemo
{ /// <summary> /// 性别枚举 /// </summary> public enum Gender { 男, 女
}
} |
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace GenericDemo
{ /// <summary> /// 员工类 /// </summary> 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 Gender Gender { get; set; }
/// <summary>
/// 人气值 /// </summary> private int _popularity = 0;
public int Popularity
{ get { return _popularity; } set { _popularity = value; } }
public string SayHi()
{ string message = string.Format("大家好,我是 {0}, 今年 {1}岁,我的人气值高达 {3}!",this.Name,this.Age,this.Popularity); return message; } } } |
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 GenericDemo { public partial class frmCardRecord : Form { public frmCardRecord() { InitializeComponent(); }
//窗体加载
private void frmCardRecord_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 GenericDemo { public partial class frmMaintance : Form { /// <summary> /// 维护类型 1表示新增,0表示修改 /// </summary> public int MaintanceType { get; set; }
//主窗体引用
public frmMain FrmParent { get; set; }
public frmMaintance()
{ InitializeComponent(); this.cmbSex.SelectedIndex = 0; }
//保存记录
private void btnSave_Click(object sender, EventArgs e) { try { #region 输入处理 SE pr = new SE(); pr.ID = this.txtWorkNO.Text.Trim(); pr.Age = Int32.Parse(this.txtAge.Text.Trim()); if (this.cmbSex.SelectedItem.ToString() == "男") { pr.Gender = Gender.男; } else { pr.Gender = Gender.女; } pr.Name = this.txtName.Text.Trim(); #endregion if (this.MaintanceType == 1)//添加 { #region 添加操作 foreach (SE item in FrmParent.programmerList) { if (item.ID == pr.ID) { MessageBox.Show("此工号已经存在!"); return; } } FrmParent.programmerList.Add(pr); #endregion } else { #region 修改操作 #endregion } this.Close(); } catch (Exception ex) { MessageBox.Show("出错:" + ex.Message); } finally { this.FrmParent.BindGrid(FrmParent.programmerList);//刷新父窗体的信息。
}
} } } |
using System;
using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace GenericDemo
{ public partial class frmMain : Form { /// <summary> /// 列表,用于保存员工(程序员)对象 /// </summary> public List<SE> programmerList = new List<SE>(); public frmMain() { InitializeComponent(); this.dgvProgrammer.AutoGenerateColumns = false; } /// <summary> /// 绑定List中的数据到DataGridView /// </summary> public void BindGrid(List<SE> list) { this.dgvProgrammer.DataSource = new BindingList<SE>(list); } /// <summary> /// 添加按钮的响应操作
/// </summary>
/// <param name="sender"></param> /// <param name="e"></param> private void tsbtnAdd_Click(object sender, EventArgs e) { frmMaintance frm = new frmMaintance(); frm.MaintanceType = 1; frm.FrmParent = this; frm.ShowDialog(); } } } |