深入.net和C#编程第三章上机练习2

类:
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);//刷新父窗体的信息。
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值