员工修改添加,部门修改添加
一:先看看解决方案资源管理器截图:
二:DepartmentDAL.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data; using System.Data.SqlClient; namespace HRMSys.DAL { public class DepartmentDAL { ////// 将得到的表的形式转化为object对象字段的形式 /// /// /// private Department ToModel(DataRow row) { Department dept = new Department(); dept.Id = (Guid)row["Id"]; dept.Name = (string)row["Name"]; dept.IsDelete=(bool) row["IsDelete"]; return dept; } /// /// 显示所有未删除的部门信息 /// /// 以部门为对象的泛型集合 public IEnumerable ListAll() //public List ListAll() { Listlist = new List (); DataTable dt = sqlhelper.datatable("select * from T_Department where IsDelete=0"); foreach (DataRow row in dt.Rows) { Department dept = ToModel(row); list.Add(dept); } return list; } /// /// 得到指定id的部门 /// /// /// 部门对象 public Department GetById(Guid id) { DataTable dt = sqlhelper.datatable("select * from T_Department where Id=@Id", new SqlParameter("@Id", id)); if (dt.Rows.Count <= 0) { return null; } else { return ToModel(dt.Rows[0]); } } /// /// 更新指定部门id的部门名字 /// /// /// public void Update(Guid id, string name) { sqlhelper.ExecuteNon(@"Update T_Department Set Name=@Name where Id=@Id", new SqlParameter("@Name", name), new SqlParameter("@Id", id)); } /// /// 插入一条部门信息 /// /// public void Insert(string name) { sqlhelper.ExecuteNon(@"Insert Into T_Department(Id,Name,IsDelete) values(newid(),@Name,0)", new SqlParameter("@Name", name)); } /// /// 软删除指定部门id的部门 /// /// public void DeleteById(Guid id) { sqlhelper.ExecuteNon(@"Update T_Department Set IsDelete=1 where Id=@Id", new SqlParameter("@Id", id)); } } }
三:EmployeeDAL.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data; using System.Data.SqlClient; namespace HRMSys.DAL { public class EmployeeDAL { ////// 将表的列转换为EmployeeList对象的字段 /// /// /// public EmployeeList ToEmployeeListModel(DataRow row) { EmployeeList employee = new EmployeeList(); employee.Address = (string)row["Address"]; employee.BaseSalary = (int)row["BaseSalary"]; employee.BirthDay = (DateTime)row["BirthDay"]; employee.ContractEndDay = (DateTime)row["ContractEndDay"]; employee.ContractStartDay = (DateTime)row["ContractStartDay"]; employee.Department =GetNameByGuidDE( (Guid)row["DepartmentId"]);////// employee.Education= GetNameByGuidID((Guid)row["EducationId"]);///// employee.Email = (string)row["Email"]; employee.EmergencyContact = (string)sqlhelper.FromDbValue(row["EmergencyContact"]); employee.Gender = GetNameByGuidID((Guid)row["GenderId"]);/////// employee.Id = (Guid)row["Id"]; employee.IdNum = (string)row["IdNum"]; employee.InDate = (DateTime)row["InDate"]; employee.Major = (string)sqlhelper.FromDbValue(row["Major"]); employee.Marriage = GetNameByGuidID((Guid)row["MarriageId"]);///////// employee.Name = (string)row["Name"]; employee.Nationality = (string)row["Nationality"]; employee.NativeAddr = (string)row["NativeAddr"]; employee.Number = (string)row["Number"]; employee.PartyStatus = GetNameByGuidID((Guid)row["PartyStatusId"]);////// employee.Position = (string)row["Position"]; employee.Remarks = (string)sqlhelper.FromDbValue(row["Remarks"]); employee.Resume = (string)sqlhelper.FromDbValue(row["Resume"]); employee.School = (string)sqlhelper.FromDbValue(row["School"]); employee.TelNum = (string)row["TelNum"]; employee.IsStopped = (bool)row["IsStopped"]; //todo:如果员工非常多,那么Photo会增加内存占用 //employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]); return employee; } /// /// 将表转换为Employee对象的字段 /// /// /// public Employee ToEmployeeModel(DataRow row) { Employee employee = new Employee(); employee.Address = (string)row["Address"]; employee.BaseSalary = (int)row["BaseSalary"]; employee.BirthDay = (DateTime)row["BirthDay"]; employee.ContractEndDay = (DateTime)row["ContractEndDay"]; employee.ContractStartDay = (DateTime)row["ContractStartDay"]; employee.DepartmentId = (Guid)row["DepartmentId"];////// employee.EducationId = (Guid)row["EducationId"];///// employee.Email = (string)row["Email"]; employee.EmergencyContact = (string)sqlhelper.FromDbValue(row["EmergencyContact"]); employee.GenderId = (Guid)row["GenderId"];/////// employee.Id = (Guid)row["Id"]; employee.IdNum = (string)row["IdNum"]; employee.InDate = (DateTime)row["InDate"]; employee.Major = (string)sqlhelper.FromDbValue(row["Major"]); employee.MarriageId = (Guid)row["MarriageId"];///////// employee.Name = (string)row["Name"]; employee.Nationality = (string)row["Nationality"]; employee.NativeAddr = (string)row["NativeAddr"]; employee.Number = (string)row["Number"]; employee.PartyStatusId = (Guid)row["PartyStatusId"];////// employee.Position = (string)row["Position"]; employee.Remarks = (string)sqlhelper.FromDbValue(row["Remarks"]); employee.Resume = (string)sqlhelper.FromDbValue(row["Resume"]); employee.School = (string)sqlhelper.FromDbValue(row["School"]); employee.TelNum = (string)row["TelNum"]; employee.IsStopped = (bool)row["IsStopped"]; //todo:如果员工非常多,那么Photo会增加内存占用 //employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]); return employee; } /// /// 软删除指定id的员工信息 /// /// public void DeleteById(Guid id) { //update T_Operator set IsLocked=@IsLocked where Id=@Id" int i= sqlhelper.ExecuteNon("update T_Employee set IsStopped =1 where Id=@Id", new SqlParameter ("@Id",id)); } /// /// 指定id从idname中取得名字 /// /// /// public string GetNameByGuidID(Guid id) { DataTable table= sqlhelper.datatable("select Name from T_IdName where Id=@Id", new SqlParameter("@Id",id)); DataRow row = table.Rows[0]; return (string) row["Name"]; } /// /// 指定id从department中取得名字 /// /// /// public string GetNameByGuidDE(Guid id) { DataTable table = sqlhelper.datatable("select Name from T_Department where Id=@Id", new SqlParameter("@Id", id)); DataRow row = table.Rows[0]; return (string)row["Name"]; } /// /// 得到所有未删除的员工的所有信息 /// /// Employee数组 public EmployeeList[] ListAll() { DataTable table = sqlhelper.datatable("select * from T_Employee where IsStopped=0"); EmployeeList[] items = new EmployeeList[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { EmployeeList employee = ToEmployeeListModel(table.Rows[i]); items[i] = employee; } return items; } /// /// 得到指定id的员工信息 /// /// /// Employee对象 public Employee GetById(Guid id) { DataTable table = sqlhelper.datatable("select * from T_Employee where Id=@Id", new SqlParameter("@Id", id)); if (table.Rows.Count == 1) { return ToEmployeeModel(table.Rows[0]); } else { throw new Exception(); } } /// /// 插入一条员工数据 /// /// public void Insert(Employee employee) { sqlhelper.ExecuteNon(@"INSERT INTO [T_Employee] ([Id],[Number],[Name],[BirthDay],[InDate],[MarriageId],[PartyStatusId],[Nationality] ,[NativeAddr],[EducationId],[Major],[School],[Address],[BaseSalary],[Email] ,[IdNum],[TelNum],[EmergencyContact],[DepartmentId],[Position],[ContractStartDay] ,[ContractEndDay],[Resume],[Remarks],[IsStopped],[GenderId]) VALUES(newid(),@Number,@Name,@BirthDay,@InDate,@MarriageId,@PartyStatusId,@Nationality ,@NativeAddr,@EducationId,@Major,@School,@Address,@BaseSalary,@Email ,@IdNum,@TelNum,@EmergencyContact,@DepartmentId,@Position,@ContractStartDay ,@ContractEndDay,@Resume,@Remarks,0,@GenderId)", new SqlParameter("@Number", employee.Number) , new SqlParameter("@Name", employee.Name) , new SqlParameter("@BirthDay", employee.BirthDay) , new SqlParameter("@InDate", employee.InDate) , new SqlParameter("@MarriageId", employee.MarriageId) , new SqlParameter("@PartyStatusId", employee.PartyStatusId) , new SqlParameter("@Nationality", employee.Nationality) , new SqlParameter("@NativeAddr", employee.NativeAddr) , new SqlParameter("@EducationId", employee.EducationId) , new SqlParameter("@Major", sqlhelper.ToDbValue(employee.Major)) , new SqlParameter("@School", sqlhelper.ToDbValue(employee.School)) , new SqlParameter("@Address", employee.Address) , new SqlParameter("@BaseSalary", employee.BaseSalary) , new SqlParameter("@Email", sqlhelper.ToDbValue(employee.Email)) , new SqlParameter("@IdNum", employee.IdNum) , new SqlParameter("@TelNum", employee.TelNum) , new SqlParameter("@EmergencyContact", sqlhelper.ToDbValue(employee.EmergencyContact)) , new SqlParameter("@DepartmentId", employee.DepartmentId) , new SqlParameter("@Position", employee.Position) , new SqlParameter("@ContractStartDay", employee.ContractStartDay) , new SqlParameter("@ContractEndDay", employee.ContractEndDay) , new SqlParameter("@Resume", sqlhelper.ToDbValue(employee.Resume)) , new SqlParameter("@Remarks", sqlhelper.ToDbValue(employee.Remarks)) , new SqlParameter("@GenderId", employee.GenderId) ); } /// /// 更新一条员工数据数据 /// /// public void Update(Employee employee) { sqlhelper.ExecuteNon(@"Update T_Employee set [Number]=@Number,[Name]=@Name,[BirthDay]=@BirthDay,[InDate]=@InDate, [MarriageId]=@MarriageId,[PartyStatusId]=@PartyStatusId,[Nationality]=@Nationality, [NativeAddr]=@NativeAddr,[EducationId]=@EducationId,[Major]=@Major,[School]=@School, [Address]=@Address,[BaseSalary]=@BaseSalary,[Email]=@Email, [IdNum]=@IdNum,[TelNum]=@TelNum,[EmergencyContact]=@EmergencyContact, [DepartmentId]=@DepartmentId,[Position]=@Position,[ContractStartDay]=@ContractStartDay, [ContractEndDay]=@ContractEndDay,[Resume]=@Resume,[Remarks]=@Remarks,[GenderId]=@GenderId Where Id=@Id", new SqlParameter("@Number", employee.Number) , new SqlParameter("@Name", employee.Name) , new SqlParameter("@BirthDay", employee.BirthDay) , new SqlParameter("@InDate", employee.InDate) , new SqlParameter("@MarriageId", employee.MarriageId) , new SqlParameter("@PartyStatusId", employee.PartyStatusId) , new SqlParameter("@Nationality", employee.Nationality) , new SqlParameter("@NativeAddr", employee.NativeAddr) , new SqlParameter("@EducationId", employee.EducationId) , new SqlParameter("@Major", sqlhelper.ToDbValue(employee.Major)) , new SqlParameter("@School", sqlhelper.ToDbValue(employee.School)) , new SqlParameter("@Address", employee.Address) , new SqlParameter("@BaseSalary", employee.BaseSalary) , new SqlParameter("@Email", sqlhelper.ToDbValue(employee.Email)) , new SqlParameter("@IdNum", employee.IdNum) , new SqlParameter("@TelNum", employee.TelNum) , new SqlParameter("@EmergencyContact", sqlhelper.ToDbValue(employee.EmergencyContact)) , new SqlParameter("@DepartmentId", employee.DepartmentId) , new SqlParameter("@Position", employee.Position) , new SqlParameter("@ContractStartDay", employee.ContractStartDay) , new SqlParameter("@ContractEndDay", employee.ContractEndDay) , new SqlParameter("@Resume", sqlhelper.ToDbValue(employee.Resume)) , new SqlParameter("@Remarks", sqlhelper.ToDbValue(employee.Remarks)) , new SqlParameter("@GenderId", employee.GenderId) , new SqlParameter("@Id", employee.Id)); } } }
四:IdNameDAL.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data.SqlClient; using System.Data; namespace HRMSys.DAL { public class IdNameDAL { //按分类得到所要的数据 public IdName[] GetByCategery(string categery) { DataTable table= sqlhelper.datatable("select Id,Name from T_IdName where Categery=@Categery", new SqlParameter("@Categery", categery)); IdName[] idnam = new IdName[table.Rows.Count] ; for (int i = 0; i < table.Rows.Count; i++) { DataRow row=table.Rows[i]; IdName idname = new IdName(); idname.Id=(Guid) row["Id"]; idname.Name=(string)row["Name"]; idnam[i] = idname; } return idnam; } } }
五:sqlhelper.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace HRMSys.DAL { static class sqlhelper { static SqlConnection conn; static SqlCommand cmd; ////// 返回受影响的行数 /// public static readonly string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; public static int ExecuteNon(string sql,params SqlParameter[] parameter ) { using (conn = new SqlConnection(constr)) { conn.Open(); using ( cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); return cmd.ExecuteNonQuery(); } } } /// ///返回结果的首行首列 /// /// /// /// public static int ExecuteSca(string sql, params SqlParameter[] parameter) { using (conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); int result = (int) cmd.ExecuteScalar(); return result; } } } /// /// 返回一个数据流 /// /// /// /// public static object SqldataRea(string sql, params SqlParameter[] parameter) { using ( conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); SqlDataReader sdr= sdr=cmd.ExecuteReader(); return sdr; } } } /// /// 返回一个表 /// /// /// /// public static DataTable datatable(string sql, params SqlParameter[] parameter) { using ( conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); return ds.Tables[0]; } } } /// /// 可空的处理,将数据库的可空类型转换为vs里的可空类型 /// /// /// public static object FromDbValue(object value) { if (value == DBNull.Value) { return null; } else { return value; } } /// /// 将vs里的可空类型转换为数据库里的可空类型 /// /// /// public static object ToDbValue(object value) { if (value == null) { return DBNull.Value; } else { return value; } } } }
六:Department.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class Department { public Guid Id { get; set; } public string Name { get; set; } public bool IsDelete { get; set; } } }
七:Employee.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class Employee { public Guid Id { get; set; } ////// 工号 /// public string Number { get; set; } public string Name { get; set; } /// /// 性别Id /// public Guid GenderId { get; set; } //因为图片如果一次性读取到Model中会非常耗内存,因此需要的时候再去单独读取,不在Model中建字段 /// /// 出生日期 /// public DateTime BirthDay { get; set; } /// /// 入职日期 /// public DateTime InDate { get; set; } /// /// 婚姻状态Id /// public Guid MarriageId { get; set; } /// /// 政治面貌Id /// public Guid PartyStatusId { get; set; } /// /// 民族 /// public string Nationality { get; set; } /// /// 籍贯 /// public string NativeAddr { get; set; } /// /// 教育程度Id /// public Guid EducationId { get; set; } /// /// 专业 /// public string Major { get; set; } /// /// 毕业院校 /// public string School { get; set; } /// /// 地址 /// public string Address { get; set; } /// /// 基本工资 /// public int BaseSalary { get; set; } /// /// Email /// public string Email { get; set; } /// /// 有效身份证号 /// public string IdNum { get; set; } /// /// 联系电话 /// public string TelNum { get; set; } /// /// 紧急联系人信息 /// public string EmergencyContact { get; set; } /// /// 部门Id /// public Guid DepartmentId { get; set; } /// /// 职位 /// public string Position { get; set; } /// /// 合同起始时间 /// public DateTime ContractStartDay { get; set; } /// /// 合同到期时间 /// public DateTime ContractEndDay { get; set; } /// /// 简历 /// public string Resume { get; set; } /// /// 备注 /// public string Remarks { get; set; } /// /// 照片 /// //public byte[] Photo //{ // get; // set; //} public bool IsStopped { get; set; } } }
八:EmployeeList.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class EmployeeList { public Guid Id { get; set; } ////// 工号 /// public string Number { get; set; } public string Name { get; set; } /// /// 性别 /// public string Gender { get; set; } //因为图片如果一次性读取到Model中会非常耗内存,因此需要的时候再去单独读取,不在Model中建字段 /// /// 出生日期 /// public DateTime BirthDay { get; set; } /// /// 入职日期 /// public DateTime InDate { get; set; } /// /// 婚姻状态 /// public string Marriage { get; set; } /// /// 政治面貌 /// public string PartyStatus { get; set; } /// /// 民族 /// public string Nationality { get; set; } /// /// 籍贯 /// public string NativeAddr { get; set; } /// /// 教育程度 /// public string Education { get; set; } /// /// 专业 /// public string Major { get; set; } /// /// 毕业院校 /// public string School { get; set; } /// /// 地址 /// public string Address { get; set; } /// /// 基本工资 /// public int BaseSalary { get; set; } /// /// Email /// public string Email { get; set; } /// /// 有效身份证号 /// public string IdNum { get; set; } /// /// 联系电话 /// public string TelNum { get; set; } /// /// 紧急联系人信息 /// public string EmergencyContact { get; set; } /// /// 部门 /// public string Department { get; set; } /// /// 职位 /// public string Position { get; set; } /// /// 合同起始时间 /// public DateTime ContractStartDay { get; set; } /// /// 合同到期时间 /// public DateTime ContractEndDay { get; set; } /// /// 简历 /// public string Resume { get; set; } /// /// 备注 /// public string Remarks { get; set; } /// /// 照片 /// //public byte[] Photo //{ // get; // set; //} public bool IsStopped { get; set; } } }
九:IdName.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class IdName { public Guid Id { get;set;} public string Name{get;set;} } }
十:DeptEditWindow.cs源代码
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; using HRMSys.DAL; namespace HYMSys.UI.SystemMgr { public partial class DeptEditWindow : Form { public DeptEditWindow() { InitializeComponent(); } public bool IsAdd { get; set; } public Guid DepartmentId { get; set; } public bool load { get; set; } ////// 保存 /// /// /// private void button1_Click(object sender, EventArgs e) { if (IsAdd == true) { DepartmentDAL dal = new DepartmentDAL(); dal.Insert(tb_Dename.Text); MessageBox.Show("插入成功"); } else { DepartmentDAL dal = new DepartmentDAL(); dal.Update(DepartmentId, tb_Dename.Text); MessageBox.Show("修改成功"); } } /// /// 取消 /// /// /// private void button2_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); this.Close(); } /// /// 窗口关闭设置刷新list窗口的参数为true /// /// /// private void DeptEditWindow_FormClosing(object sender, FormClosingEventArgs e) { load = true; } } }
十一:DeptMgrWindow.cs源代码
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; using HRMSys.DAL; namespace HYMSys.UI.SystemMgr { public partial class DeptMgrWindow : Form { public DeptMgrWindow() { InitializeComponent(); } private void load() { DepartmentDAL de = new DepartmentDAL(); dataGridView1.DataSource = de.ListAll(); } ////// 窗体自动加载部门信息 /// /// /// private void DeptMgrWindow_Load(object sender, EventArgs e) { load(); } /// /// 部门添加 /// /// /// private void toolsb_add_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); ed.IsAdd = true; ed.ShowDialog(); if (ed.load == true) { //ed.load这个是刚新建的窗口的字段 load();//这个是本类中的方法 } } /// /// 部门删除 /// /// /// private void toolsb_delete_Click(object sender, EventArgs e) { Guid id = (Guid)dataGridView1.CurrentRow.Cells[0].Value; string name = (string)dataGridView1.CurrentRow.Cells[1].Value; DepartmentDAL da = new DepartmentDAL(); //MessageBox.Show("真的要删除--"+name+"--吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.Yes)是Yes将始终不成立,不会删除 if (MessageBox.Show("真的要删除--"+name+"--吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.OK) { da.DeleteById(id); load(); MessageBox.Show(name+"已删除"); } } /// /// 部门修改 /// /// /// private void toolsb_edit_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); ed.DepartmentId=(Guid)dataGridView1.CurrentRow.Cells[0].Value; ed.IsAdd = false; ed.ShowDialog(); if (ed.load == true) { load(); } } } }
十二:EmployeeEditWindow.cs源代码
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; using HRMSys.DAL; using HRMSys.Model; namespace HYMSys.UI.EmployeeMgr//不要将文件夹的名字写的和model的名字一样,这样就将命名空间和model重名了 { public partial class EmployeeEditWindow : Form { public EmployeeEditWindow() { InitializeComponent(); } public bool IsAdd { get; set; } public Guid EditId { get; set; } public bool IsLoad { get; set; } public bool IsSave { get; set; } Employee em = new Employee(); //得到有默认值的tb的text长度 int number; ////// 绑定数据 /// public void binding() { DepartmentDAL dal = new DepartmentDAL(); IdNameDAL idName = new IdNameDAL(); cb_EducationId.DataSource = idName.GetByCategery("学历"); cb_GenderId.DataSource = idName.GetByCategery("性别"); cb_MarriageId.DataSource = idName.GetByCategery("婚姻状况"); cb_PartyStatusId.DataSource = idName.GetByCategery("政治面貌"); cb_DepartmentId.DataSource = dal.ListAll(); IsSave = true; //部门id cb_DepartmentId.DisplayMember = "Name"; cb_DepartmentId.ValueMember = "Id"; if (em.DepartmentId != null) cb_DepartmentId.SelectedValue = em.DepartmentId; //学历id cb_EducationId.DisplayMember = "Name"; cb_EducationId.ValueMember = "Id"; if (em.DepartmentId != null)//如果不为空将选择好的数据绑定上 cb_EducationId.SelectedValue = em.EducationId; //性别id cb_GenderId.DisplayMember = "Name"; cb_GenderId.ValueMember = "Id"; if (em.DepartmentId != null) cb_GenderId.SelectedValue = em.GenderId; //婚姻状况id cb_MarriageId.DisplayMember = "Name"; cb_MarriageId.ValueMember = "Id"; if (em.DepartmentId != null) cb_MarriageId.SelectedValue = em.MarriageId; //政治面貌id cb_PartyStatusId.DisplayMember = "Name"; cb_PartyStatusId.ValueMember = "Id"; if (em.DepartmentId != null) cb_PartyStatusId.SelectedValue = em.PartyStatusId; //其他的数据绑定 tb_Address.DataBindings.Add("Text", em, "Address", false, DataSourceUpdateMode.OnPropertyChanged); tb_BaseSalary.DataBindings.Add("Text", em, "BaseSalary", false, DataSourceUpdateMode.OnPropertyChanged); dtp_BirthDay.DataBindings.Add("Value", em, "BirthDay", true, DataSourceUpdateMode.OnPropertyChanged); dtp_ContractEndDay.DataBindings.Add("Value", em, "ContractEndDay", true, DataSourceUpdateMode.OnPropertyChanged); dtp_ContractStartDay.DataBindings.Add("Value", em, "ContractStartDay", true, DataSourceUpdateMode.OnPropertyChanged); //dtp_ContractStartDay.DataBindings.Add("Value", em, "ContractStartDay", true); tb_Email.DataBindings.Add("Text", em, "Email", false, DataSourceUpdateMode.OnPropertyChanged); tb_EmergencyContact.DataBindings.Add("Text", em, "EmergencyContact", false, DataSourceUpdateMode.OnPropertyChanged); tb_IdNum.DataBindings.Add("Text", em, "IdNum", false, DataSourceUpdateMode.OnPropertyChanged); dtp_Indate.DataBindings.Add("Value", em, "InDate", true, DataSourceUpdateMode.OnPropertyChanged); tb_Major.DataBindings.Add("Text", em, "Major", false, DataSourceUpdateMode.OnPropertyChanged); tb_Name.DataBindings.Add("Text", em, "Name", false, DataSourceUpdateMode.OnPropertyChanged); tb_Nationality.DataBindings.Add("Text", em, "Nationality", false, DataSourceUpdateMode.OnPropertyChanged); tb_NativeAddr.DataBindings.Add("Text", em, "NativeAddr", false, DataSourceUpdateMode.OnPropertyChanged); tb_Number.DataBindings.Add("Text", em, "Number", false, DataSourceUpdateMode.OnPropertyChanged); //pb_Photo.DataBindings.Add(new Binding("pb_Photo", em, "Photo", true)); tb_Position.DataBindings.Add("Text", em, "Position", false, DataSourceUpdateMode.OnPropertyChanged); tb_Remarks.DataBindings.Add("Text", em, "Remarks", false, DataSourceUpdateMode.OnPropertyChanged); //tabControl1.TabPages[2].DataBindings.Add("Text", em, "Resume", true, DataSourceUpdateMode.OnPropertyChanged); tb_school.DataBindings.Add("Text", em, "School", false, DataSourceUpdateMode.OnPropertyChanged); tb_TelNum.DataBindings.Add("Text", em, "TelNum", false, DataSourceUpdateMode.OnPropertyChanged); } /// /// 窗口关闭时将刷新置为true /// /// /// private void EmployeeEditWindow_FormClosing(object sender, FormClosingEventArgs e) { IsLoad = true; } /// /// 添加时自动载入默认值,数据绑定和初始化 /// /// /// private void EmployeeEditWindow_Load(object sender, EventArgs e) { if (IsAdd==true) { #region //combobox的数据绑定 binding(); //将合同到期日在今天的基础上加1年 dtp_ContractEndDay.Value = DateTime.Now.AddYears(1); dtp_BirthDay.Value = DateTime.Now.AddYears(-24); dtp_ContractStartDay.Value = DateTime.Now; dtp_Indate.Value = DateTime.Now; tb_Nationality.Text = "汉族"; tb_BaseSalary.Text = "3000"; tb_Email.Text = "@qq.com"; tb_Number.Text = "HMJ"; number = tb_Number.Text.Length; #endregion } else { Employee list = new Employee(); EmployeeDAL dal = new EmployeeDAL(); list = dal.GetById(EditId); em = list; binding(); } } /// /// textboxToRed将背景颜色变红 /// /// private void textboxToRed(TextBox tb,int length) { if (tb.Text.Length <= length) { tb.BackColor = Color.Red;//将背景变为红色 IsSave = false; } else { tb.BackColor = DefaultBackColor;//将背景置为默认颜色 } } /// /// ComboxToRed将背景颜色变红 /// /// private void ComboxToRed(ComboBox cb) { if (cb.Text.Length <= 0) { cb.BackColor = Color.Red;//将背景变为红色 IsSave = false; } else { cb.BackColor = DefaultBackColor;//将背景置为默认颜色 } } /// /// 保存 /// /// /// private void button1_Click(object sender, EventArgs e) { if (IsAdd) { #region //检查必填项,如果为空显示为红色 textboxToRed(tb_Address, 0); textboxToRed(tb_IdNum, 0); textboxToRed(tb_Name, 0); textboxToRed(tb_Number, number);//工号和默认值长度比较 textboxToRed(tb_Position, 0); textboxToRed(tb_TelNum, 0); textboxToRed(tb_NativeAddr, 0); ComboxToRed(cb_DepartmentId); ComboxToRed(cb_EducationId); ComboxToRed(cb_GenderId); ComboxToRed(cb_MarriageId); ComboxToRed(cb_PartyStatusId); //这里不用再赋值,已经绑定了,怎么将外键id显示为对应的名字呢?cb_PartyStatusId.SelectedValue = em.PartyStatusId; #region //EmployeeDAL dal = new EmployeeDAL(); //Employee em = new Employee(); //em.EducationId = cb_EducationId.ValueMember;// //em.Address = tb_Address.Text; //em.BaseSalary = Convert.ToInt32(tb_BaseSalary.Text); //em.BirthDay = dtp_BirthDay.Value; //em.ContractEndDay = dtp_ContractEndDay.Value; //em.ContractStartDay = dtp_ContractStartDay.Value; //em.DepartmentId = cb_DepartmentId.ValueMember; //em.Email = tb_Email.Text; //em.EmergencyContact = tb_EmergencyContact.Text; //em.GenderId = cb_GenderId.ValueMember; ////em.Id=不用输的 //em.IdNum = tb_IdNum.Text; //em.InDate = dtp_Indate.Value; //em.Major = tb_Major.Text; //em.MarriageId = cb_MarriageId.ValueMember;//要转换为guid婚姻 //em.Name = tb_Name.Text; //em.Nationality = tb_Nationality.Text; //em.NativeAddr = tb_NativeAddr.Text; //em.Number = tb_Number.Text; //em.PartyStatusId = cb_PartyStatusId.ValueMember; //em.Photo = pb_Photo; //em.Position = tb_Position.Text; //em.Remarks = tb_Remarks.Text; //em.Resume = tabControl1.TabPages[2].ToString();//简历 //em.School = tb_school.Text; //em.TelNum = tb_TelNum.Text; #endregion if (IsSave == true) { EmployeeDAL darl = new EmployeeDAL(); //将数据结果给object对象 em.DepartmentId = (Guid)cb_DepartmentId.SelectedValue; em.EducationId = (Guid)cb_EducationId.SelectedValue; em.GenderId = (Guid)cb_GenderId.SelectedValue; em.MarriageId = (Guid)cb_MarriageId.SelectedValue; em.PartyStatusId = (Guid)cb_PartyStatusId.SelectedValue; darl.Insert(em); MessageBox.Show("成功插入一条员工数据"); IsLoad = true; } #endregion } else { EmployeeDAL dal = new EmployeeDAL(); dal.Update(em); MessageBox.Show("更改成功!"); } } } }
十三:EmployeeListWindow.cs源代码
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; using HRMSys.DAL; using HRMSys.Model; namespace HYMSys.UI.EmployeeMgr { public partial class EmployeeListWindow : Form { public EmployeeListWindow() { InitializeComponent(); } ////// 加载所有信息 /// public EmployeeList[] load() { EmployeeDAL de = new EmployeeDAL(); return de.ListAll(); } /// /// 添加员工 /// /// /// private void toolsb_add_Click(object sender, EventArgs e) { EmployeeEditWindow edit = new EmployeeEditWindow(); edit.IsAdd = true; edit.ShowDialog(); if (edit.IsLoad == true) { dataGridView1.DataSource = load(); } } /// /// 删除员工 /// /// /// private void toolsb_delete_Click(object sender, EventArgs e) { Guid id=(Guid)dataGridView1.CurrentRow.Cells[1].Value; string name=(string)dataGridView1.CurrentRow.Cells[3].Value; if (MessageBox.Show("真的要删除---"+name+"---吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.OK) { EmployeeDAL dal = new EmployeeDAL(); dal.DeleteById(id); dataGridView1.DataSource = load(); } } /// /// 修改员工信息 /// /// /// private void toolsb_edit_Click(object sender, EventArgs e) { EmployeeEditWindow edit = new EmployeeEditWindow(); edit.EditId = (Guid)dataGridView1.CurrentRow.Cells[1].Value; edit.IsAdd = false; edit.ShowDialog(); if (edit.IsLoad == true) { dataGridView1.DataSource = load(); } } /// /// 自动加载所有员工信息 /// /// /// private void EmployeeListWindow_Load(object sender, EventArgs e) { dataGridView1.DataSource= load(); } } }
十四:作品图