类:
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; 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(); }
/// <summary>
/// 删除按钮的相应操作
/// </summary>
/// <param name="sender"></param> /// <param name="e"></param> private void tsbtnDelete_Click(object sender, EventArgs e) { //判断是否选中一行
if(this.dgvProgrammer.SelectedRows.Count!=1)
{ MessageBox.Show("请选中一行!"); return; } //确认删除操作 DialogResult rt = MessageBox.Show("确实要删除这个员工的信息吗?","确认信息"); if (rt != DialogResult.OK) { return; } //获取当前选中行的ID string workNo = dgvProgrammer.CurrentRow.Cells["workNo"].Value.ToString(); //查找ID号对应对象并从集合中删除 foreach (SE obj in programmerList) { if (obj.ID == workNo) { this.programmerList.Remove(obj); break; } } //刷新DataGridView显示 this.BindGrid(this.programmerList); MessageBox.Show("删除成功!", "执行信息"); }
/// <summary>
/// 根据工号进行模糊查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnQuery_Click(object sender, EventArgs e) { //存储查询结果 List<SE> tempList = new List<SE>(); //查找并填充集合
foreach (SE item in this.programmerList)
{ if (item.ID.IndexOf(this.txtID.Text.Trim()) != -1) { tempList.Add(item); } } //刷新显示 this.dgvProgrammer.DataSource = new BindingList<SE>(tempList); } } } |
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);//刷新父窗体的信息。
}
} } } |
深入.net和C#编程第三章上机练习2
最新推荐文章于 2021-01-22 19:54:31 发布