当前位置: 首页 > news >正文

网站素材 图标锡林郭勒盟建设工程管理网站

网站素材 图标,锡林郭勒盟建设工程管理网站,范县网站建设公司,中国广东手机网站建设笔者入门C##xff0c;熟悉C#语法之后#xff0c;来做一个Winform项目巩固知识#xff0c;记录一下学习过程。 一、什么是Winform WinForm 是 Windows Form 的简称#xff0c;是基于 .NET Framework 平台的客户端#xff08;PC软件#xff09;开发技术#xff0c;一般…笔者入门C#熟悉C#语法之后来做一个Winform项目巩固知识记录一下学习过程。 一、什么是Winform WinForm 是 Windows Form 的简称是基于 .NET Framework 平台的客户端PC软件开发技术一般使用C#编程。C# WinForm 编程需要创建「Windows窗体应用程序」项目。 .NET 提供了大量 Windows 风格的控件和事件可以直接拿来使用上手简单开发快速 Windows 窗体应用程序是 C# 语言中的一个重要应用也是 C# 语言最常见的应用。 二、项目背景 2.1项目介绍 绩效考核管理系统主要根据企业员工的身份绩效考核来综合计算员工的薪资。 2.2涉及知识点 窗体容器、数据库操作Sql Server、数据绑定与获取、委托事件、Sql参数化、泛型反射、反射、分层架构、工厂模式、缓存、泛型缓存、单例模式、动态创建控件。 2.3数据库结构 本项目主要设计四张表人员表Users、员工绩效表UserAppraisals、身份基数表AppraisalBases、考核系数表AppraisalCoefficients。表关系如下 2.3.1人员表Users 存储员工的基本信息如姓名、性别、密码、身份Id、软删除标记 创建表格SQL语句如下 GO /****** Object: Table [dbo].[Users] Script Date: 2019/12/19 21:51:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Users]([Id] [int] IDENTITY(1,1) NOT NULL,[UserName] [varchar](50) NULL,[Sex] [varchar](8) NULL,[Password] [varchar](50) NULL,[BaseTypeId] [int] NULL,[IsDel] [bit] NULL,CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([Id] ASC )WITH (PAD_INDEX OFF, STATISTICS_NORECOMPUTE OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS ON, ALLOW_PAGE_LOCKS ON) ON [PRIMARY] ) ON [PRIMARY] 插入数据SQL语句如下  INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(张三,男,111,1,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(李四,女,111,2,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(王五,男,111,3,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(赵六1,女,111,4,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(田七,男,111,5,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(周八,女,111,6,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(吴九,女,111,6,True)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(test,男,123456,1,True)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(test2,男,111,1,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(test3,男,111,1,False)INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES(test4,男,111,1,False) 2.3.2员工绩效表UserAppraisals 用于记录所有员工的绩效情况如请假次数加班次数等 GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[UserAppraisals] Script Date: 2019/12/19 21:51:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[UserAppraisals]([Id] [int] IDENTITY(1,1) NOT NULL,[UserId] [int] NULL,[CoefficientId] [int] NULL,[Count] [float] NULL,[AssessmentYear] [int] NULL,[IsDel] [bit] NULL,CONSTRAINT [PK_UserAppraisal] PRIMARY KEY CLUSTERED ([Id] ASC )WITH (PAD_INDEX OFF, STATISTICS_NORECOMPUTE OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS ON, ALLOW_PAGE_LOCKS ON) ON [PRIMARY] ) ON [PRIMARY] INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,1,12,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,2,2,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,3,12,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,4,158,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,5,36,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(1,6,3,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,1,3,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,2,3,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,3,6,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,4,0,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,5,116,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(3,6,0,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,1,12,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,2,12,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,3,2,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,4,2,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,5,2,2018,False)INSERT INTO UserAppraisals(UserId,CoefficientId,Count,AssessmentYear,IsDel) VALUES(5,6,12,2018,False) 2.3.3身份基数表AppraisalBases 用于记录员工对应身份的基本工资如政法编制基本工资为20000元行政编制基本工资为1800元 GO /****** Object: Table [dbo].[AppraisalBases] Script Date: 2019/12/19 21:51:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[AppraisalBases]([Id] [int] IDENTITY(1,1) NOT NULL,[BaseType] [varchar](50) NULL,[AppraisalBase] [int] NULL,[IsDel] [bit] NULL,CONSTRAINT [PK_AppraisalBases_1] PRIMARY KEY CLUSTERED ([Id] ASC )WITH (PAD_INDEX OFF, STATISTICS_NORECOMPUTE OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS ON, ALLOW_PAGE_LOCKS ON) ON [PRIMARY] ) ON [PRIMARY] INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(政法编制1,20000,False)INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(行政编制,18000,False)INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(事业编制管理类,18000,False)INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(事业编制专业技术类,19800,False)INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(事业编制工勤类,16000,False)INSERT INTO AppraisalBases(BaseType,AppraisalBase,IsDel) VALUES(社会化用工,8000,False) 2.3.4考核系数表AppraisalCoefficients 存储考核方式对绩效的影响关系如影响系数计算方式-代表扣除工资代表奖励工资 GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[AppraisalCoefficients] Script Date: 2019/12/19 21:51:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[AppraisalCoefficients]([Id] [int] IDENTITY(1,1) NOT NULL,[AppraisalType] [varchar](50) NULL,[AppraisalCoefficient] [float] NULL,[CalculationMethod] [int] NULL,[IsDel] [bit] NULL,CONSTRAINT [PK_AppraisalBases] PRIMARY KEY CLUSTERED ([Id] ASC )WITH (PAD_INDEX OFF, STATISTICS_NORECOMPUTE OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS ON, ALLOW_PAGE_LOCKS ON) ON [PRIMARY] ) ON [PRIMARY]INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(请假,0.1,-1,False)INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(迟到,0.05,-1,False)INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(加班,0.1,1,False)INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(办案数量,0.0005,1,False)INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(维护次数,0.002,1,False)INSERT INTO AppraisalCoefficients(AppraisalType,AppraisalCoefficient,CalculationMethod,IsDel) VALUES(项目开发,0.3,1,False) 三、编码 3.1项目创建 右上角 文件-新建-项目选择C# Windows 桌面选项下的Windows窗体应用(.NET Framework) 3.2数据库连接 首先来了解一下C#中using关键词的三个用法 1.引用命名空间    类似java的import。可以导入其他文件中的类而不必指明其详细的命名空间。    如using System之后就可以直接用string代替System.string 2.using别名    给某个详细的命名空间中的某个类型取别名用于区分不同命名空间中名字相同的类。    如using test1A.test;        using test2B.test; 3.用来简化资源的释放    using定义了一个范围一旦超出了这个范围自动调用IDisposable释放调该对象资源    只有实现了IDisposable的类可以使用这种语法。    using(SqlConnection connnew SqlConnection(ConStr))    using实质是一个try-finally语句并在finally中调用对象的Dispose方法释放资源以防程序在出错时资源得不到释放。 连接数据库操作 1.编写连接字符串为了简洁将连接字符串写在App.config里面 connectionStringsadd nameConStr connectionStringData Source127.0.0.1;databasePerformanceAppraisalDb;uidsa;pwd123456//connectionStrings 2.创建SqlConnection对象 namespace Appraisal_System.Utility {public class SqlHelper{public static string ConStr { get; set; }public static DataTable ExecuteTable(string cmdText, params SqlParameter[] sqlParameters){using(SqlConnection connnew SqlConnection(ConStr)){conn.Open();SqlCommand cmd new SqlCommand(cmdText, conn);cmd.Parameters.AddRange(sqlParameters);SqlDataAdapter sda new SqlDataAdapter(cmd);DataSet ds new DataSet();sda.Fill(ds);return ds.Tables[0];}}public static int ExecuteNonQuery(string cmdText,params SqlParameter[] sqlParameters){using (SqlConnection conn new SqlConnection(ConStr)){conn.Open();SqlCommand cmd new SqlCommand(cmdText, conn);cmd.Parameters.AddRange(sqlParameters);int rows cmd.ExecuteNonQuery();if (rows 0){//throw new Exception(数据库操作失败);}return rows;}}} } params修饰可变数组表示传入的参数个数不定  SqlParameter类用来实现带参数的Sql语句防止Sql注入。该类表示SqlCommand的参数实际调用时可以通过SqlCommand的Parameters.Add()或AddRange()方法传入Sql参数。 3.3用户管理窗体 窗体元素如下 先介绍几个常见的窗体元素 label:是一个标签用来进行文字说明的 textBox:是一个输入框输入文字。类似于html中的input标签中的text comboBox下拉框常用于在多个项之间进行选择如选择城市 checkBox:单选框 dataGridView:数据表格以表格的形式展现数据 button:按钮 在dataGridView中添加列 编写AppraisalBases充血模型代码 充血模型和贫血模型区别 充血模型 数据和对应的业务逻辑被封装到同一个类中。因此这种充血模型满足面向对象的封装特性是典型的面向对象编程风格。业务逻辑集中在 Service 类中。基于充血模型Service 层包含 Service 类和 Domain 类两部分。Domain 是基于充血模型开发的既包含数据也包含业务逻辑。而 Service 类变得非常单薄。 充血模型中绝大多业务逻辑都应该被放在domain里面包括持久化逻辑而Service层是很薄的一层仅仅封装事务和少量逻辑不和DAO层打交道。service 组合服务也叫事务服务model除包含get set方法还包含原子服务和数据持久化的逻辑 贫血模型 贫血模型是一种领域模型其中领域对象包含很少或没有业务逻辑。是一种面向过程的编程模式它与面向对象设计的基本思想相悖将数据和过程结合在一起。因为贫血模型没有逻辑实现所以逻辑基本上会放到调用贫血模型的service中这些service类会转换领域对象的状态。贫血模型中domain包含了不依赖于持久化的原子领域逻辑而组合逻辑在Service层。service 组合服务也叫事务服务。model除包含get set方法还包含原子服务如获得关联model的id。dao数据持久化。   AppraisalBases实体类如下 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using Appraisal_System.Utility; namespace Appraisal_System.Models {public class AppraisalBases{public int Id { get; set; }public string BaseType { get; set; }public int AppraisalBase { get; set; }public bool IsDel { get; set; }public static ListAppraisalBases ListAll(){ListAppraisalBases list new ListAppraisalBases();DataTable dt SqlHelper.ExecuteTable(SELECT * FROM AppraisalBases);foreach (DataRow dr in dt.Rows){list.Add(dr.DataRowToModelAppraisalBases());}return list;}private static AppraisalBases ToModel(DataRow dr){AppraisalBases appraisalBase new AppraisalBases();appraisalBase.Id (int)dr[Id];appraisalBase.BaseType dr[BaseType].ToString();appraisalBase.AppraisalBase (int)dr[AppraisalBase];appraisalBase.IsDel (bool)dr[IsDel];return appraisalBase;}public static int Update(AppraisalBases appraisal){string sql UPDATE AppraisalBases SET BaseTypeBaseType,AppraisalBaseAppraisalBase,IsDelIsDel WHERE IdId;int rows SqlHelper.ExecuteNonQuery(sql,new SqlParameter(Id, appraisal.Id),new SqlParameter(BaseType, appraisal.BaseType),new SqlParameter(AppraisalBase, appraisal.AppraisalBase),new SqlParameter(IsDel, appraisal.IsDel));return rows;}} }Users实体类如下  using Appraisal_System.Utility; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Appraisal_System.Models {public class Users{public int Id { get; set; }public string UserName { get; set; }public string PassWord { get; set; }public string Sex { get; set; }public int BaseTypeId { get; set; }public bool IsDel { get; set; }public static ListUsers ListAll(){ListUsers list new ListUsers();string sql SELECT u.Id,u.UserName,u.PassWord,u.Sex,u.BaseTypeId,u.IsDel FROM Users u;DataTable dt SqlHelper.ExecuteTable(sql);foreach (DataRow dr in dt.Rows){list.Add(dr.DataRowToModelUsers());}return list;}public static int Insert(Users user){string sql $INSERT INTO Users(UserName,PassWord,Sex,BaseTypeId,IsDel) VALUES(UserName,PassWord,Sex,BaseTypeId,IsDel);//using SqlParameter to set query paramsreturn SqlHelper.ExecuteNonQuery(sql,new SqlParameter(UserName, user.UserName),new SqlParameter(PassWord, user.PassWord),new SqlParameter(Sex, user.Sex),new SqlParameter(BaseTypeId, user.BaseTypeId),new SqlParameter(IsDel, user.IsDel));}public static int Update(Users user){string sql $UPDATE Users SET UserNameUserName,PassWordPassWord,SexSex,BaseTypeIdBaseTypeId,IsDelIsDel WHERE IdId;//using SqlParameter to set query paramsreturn SqlHelper.ExecuteNonQuery(sql,new SqlParameter(UserName, user.UserName),new SqlParameter(PassWord, user.PassWord),new SqlParameter(Sex, user.Sex),new SqlParameter(BaseTypeId, user.BaseTypeId),new SqlParameter(IsDel, user.IsDel),new SqlParameter(Id, user.Id));}}}整个窗体代码  using Appraisal_System.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System {public delegate void DelBindDgv();//声明一个类似string/int的关键字DelBindDgv delBindDgvpublic partial class FrmUserManager : Form{DelBindDgv delBindDgv;public FrmUserManager(){InitializeComponent();}private void FrmUserManager_Load(object sender, EventArgs e){BindCbx();BindDgv();delBindDgv BindDgv;}private void BindDgv(){string userName txtUserName.Text.Trim();int baseTypeId (int)cbxBase.SelectedValue;bool isStop chkIsStop.Checked;ListUserAppraisalBases userAppraisalBases UserAppraisalBases.GetListJoinAppraisal();dgvUserAppraisal.AutoGenerateColumns false;if (baseTypeId 0){dgvUserAppraisal.DataSource userAppraisalBases.FindAll(m m.UserName.Contains(userName) m.IsDel isStop);}else{dgvUserAppraisal.DataSource userAppraisalBases.FindAll(m m.UserName.Contains(userName) m.BaseTypeId baseTypeId m.IsDel isStop);}}private void BindCbx(){ListAppraisalBases appraisalBases new ListAppraisalBases();//add an initial choice for querying allappraisalBases.Add(new AppraisalBases{Id 0,BaseType -查询所有-,AppraisalBase 0,IsDel false});appraisalBases.AddRange(AppraisalBases.ListAll());//set dataSource appraisalBases which queried cbxBase.DataSource appraisalBases;//DisplayMember means what we see on the frameworkcbxBase.DisplayMember BaseType;//ValueMember means its value such as Id;cbxBase.ValueMember id;/*cbxBase.Text -查询所有-;cbxBase.Items.Add(-查询所有-);foreach (var appraisalBase in appraisalBases){cbxBase.Items.Add(appraisalBase.BaseType);}*/}private void button1_Click(object sender, EventArgs e){BindDgv();}private void dgvUserAppraisal_MouseDown(object sender, MouseEventArgs e){if(e.Button MouseButtons.Right){tsmAdd.Visible true;tsmEdit.Visible false;tsmStart.Visible false;tsmStop.Visible false;}}private void dgvUserAppraisal_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e){if (e.Button MouseButtons.Right){if (e.RowIndex -1){dgvUserAppraisal.Rows[e.RowIndex].Selected true;tsmAdd.Visible true;tsmEdit.Visible true;bool IsDel (bool)dgvUserAppraisal.SelectedRows[0].Cells[IsDel].Value;if (IsDel){tsmStart.Visible true;}else{tsmStop.Visible true;}}}}private void tsmAdd_Click(object sender, EventArgs e){FrmSetUser frmSetUsernew FrmSetUser(delBindDgv);frmSetUser.ShowDialog();//Reload bindDgv data to refresh the latest data which was inserted just nowBindDgv();}private void tsmEdit_Click(object sender, EventArgs e){int userId (int)dgvUserAppraisal.SelectedRows[0].Cells[Id].Value;FrmSetUser frmSetUser new FrmSetUser(delBindDgv,userId);frmSetUser.ShowDialog();}} }编写用户管理窗体右键事件出现新建、编辑、启用、停用子菜单 子菜单元素为ContextMeauStrip 新建、编辑窗体  using Appraisal_System.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System {public partial class FrmSetUser : Form{private DelBindDgv _delBindDgv;public FrmSetUser(DelBindDgv delBindDgv){InitializeComponent();_delBindDgv delBindDgv;}private Users _user;public FrmSetUser(DelBindDgv delBindDgv,int userId):this(delBindDgv){//:this(args) like java this(args) but in C# should be after method and java in method_user Users.ListAll().Find(m m.Id userId);}private void FrmSetUser_Load(object sender, EventArgs e){ListAppraisalBases appraisalBases new ListAppraisalBases();//add an initial choice for querying allappraisalBases.AddRange(AppraisalBases.ListAll());//set dataSource appraisalBases which queried cbxBase.DataSource appraisalBases;//displayMember means what we see on the frameworkcbxBase.DisplayMember BaseType;//displayMember means its value such as Id;cbxBase.ValueMember Id;//load the user which is checked nowif (_user ! null){txtUserName.Text _user.UserName;cbxBase.SelectedValue _user.BaseTypeId;cbxSex.Text _user.Sex;chkIsStop.Checked _user.IsDel;}}private void btnmSave_Click(object sender, EventArgs e){string userName txtUserName.Text.Trim();int baseTypeId (int)cbxBase.SelectedValue;string sex cbxSex.Text;bool isDel chkIsStop.Checked;if (_user null){Users user new Users{UserName userName,BaseTypeId baseTypeId,IsDel isDel,Sex sex,PassWord 111,};Users.Insert(user);MessageBox.Show(用户添加成功!);}else{_user.UserName userName;_user.BaseTypeId baseTypeId;_user.IsDel isDel;_user.Sex sex;Users.Update(_user);MessageBox.Show(用户修改成功!);}_delBindDgv(); this.Close();}} } FrmSetUser中用到了委托机制。委托简而言之就是将某个类的某个方法委托给其他类执行需要Delegate关键字。 委托四部曲 1、声明委托类型 2、有一个方法包含了执行的代码 3、创建委托实例 4、调用委托实例 父类将委托对象作为构造方法中的参数传递给子类  子类在创建的时候调用自己的构造方法绑定委托对象。子类就可以在自己内部调用该方法。 因为子窗体是用来新建和编辑用户的那么子窗体在提交的时候需要进行表单的数据刷新而表单的数据刷新是由父窗体中的BindDgv方法完成的。子窗体通过调用委托实现了间接调用了父窗体中BindDgv的方法。 3.4基数管理窗体 工厂模式反射泛型缓存优化窗体的创造 场景:左侧树形菜单点击进行窗体切换时先前都是每切换一次就新建一个窗体复用性差性能低下。 解决方案将窗体的创造交给FormFactory工厂类去执行且每个窗体设计成单例模式缓存在ListForm中。创造窗体时利用反射创造即根据传入的窗体类名创造出对应的窗体类。 Assembly关键词 通过Assembly可以动态加载程序集并查看程序集的内部信息其中最常用的就是Load()这个方法。      Assembly assemblyAssembly.Load(MyAssembly);      利用Assembly的object CreateInstance(string) 方法可以反射创建一个对象参数0为类名。 Type关键词      Type是最常用到的类通过Type可以得到一个类的内部信息也可以通过它反射创建一个对象。一般有三个常用的方法可得到Type对象。 Activator关键词      需要动态的创建一个实例模型的时候就用Activator.CreateInstance(Type type)      以下两种方法区别仅为创建无参数的构造方法和创建有参数的构造函数。 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Object[]) using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System.Common {public class FormFactory{//single instanceprivate static Form form;private static ListForm forms new ListForm();private static ListType types;static FormFactory(){Assembly ass Assembly.LoadFrom(Appraisal_System.exe);types ass.GetTypes().ToList();}public static Form CreateForm(string formName){//get by reflecting by name of c#//Assembly assAssembly.Load(Appraisal_System);//get by path LoadFile of absolute path or LoadFrom of current path//string path AppDomain.CurrentDomain.BaseDirectory;//Assembly ass1 Assembly.LoadFile(path Appraisal_System.exe);HideFormAll();formName formName null ? FrmNone : formName;Form form forms.Find(mm.NameformName);//avoid to create the same form too many times by checking the formsif (form null){Type type types.Find(m m.Name formName);form (Form)Activator.CreateInstance(type);forms.Add(form);}return form;}public static void HideFormAll(){foreach (Form f in forms){f.Hide();}}} }基数管理窗体的实现代码 using Appraisal_System.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System {public partial class FrmBaseManager : Form{public FrmBaseManager(){InitializeComponent();}private void FrmBaseManager_Load(object sender, EventArgs e){dgvBase.DataSource AppraisalBases.ListAll();}private void dgvBase_CellValueChanged(object sender, DataGridViewCellEventArgs e){AppraisalBases appraisal(AppraisalBases)dgvBase.Rows[e.RowIndex].DataBoundItem;AppraisalBases.Update(appraisal);}} } 3.5系数管理窗体 3.6人员绩效窗体 人员绩效窗体是用来显示所有员工的绩效明细。包括所有考核项以及年终奖。 子窗体用来编辑考核项 观察表中可知任何一个考核项如请假、迟到、加班都会影响实发年终奖。而每个考核项有考核名称、系数、次数、计算方式。根据需求系数、次数、计算方式这些是隐藏不显示在表格中的利用表格横向拓展来填充表格。 基本表在此表上做横向拓展。 SELECT u.Id,u.UserName,u.Sex,u.BaseTypeId,a.BaseType,a.AppraisalBase,u.IsDel FROM Users u LEFT JOIN AppraisalBases a ON u.BaseTypeIda.Id 其次需要知道每个员工的各项考核项情况。将UserAppraisals表和AppraisalCoefficients联表查询。  那么只要我们在上述表中对UserId和AssessmentYear筛选就可以知道指定用户在某一年的所有考核项情况。如对于UserId1的员工和AssessmentYear2018情况如下 可以看到UserId1的员工在2018年请假了16次迟到2次加班3次等等。根据这些就可以计算得出该用户在某年的总年终奖。  上述两个实体类AppraisalCoefficients以及UserAppraisalCoefficients代码如下 using Appraisal_System.Utility; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Appraisal_System.Models {public class AppraisalCoefficients{public int Id { get; set; }public string AppraisalType { get; set; }public double AppraisalCoefficient { get; set; }public int CalculationMethod { get; set; }public bool IsDel { get; set; }public static ListAppraisalCoefficients ListAll(){ListAppraisalCoefficients listnew ListAppraisalCoefficients();DataTable dt SqlHelper.ExecuteTable(SELECT * FROM AppraisalCoefficients);foreach (DataRow dr in dt.Rows){list.Add(dr.DataRowToModelAppraisalCoefficients());}return list;}} }using Appraisal_System.Utility; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Appraisal_System.Models {public class UserAppraisalCoefficients{public int Id { get; set; }public int UserId { get; set; }public int CoefficientId { get; set; }public double Count { get; set; }public int AssessmentYear { get; set; }public string AppraisalType { get; set; }public double AppraisalCoefficient { get; set; }public int CalculationMethod { get; set; }public bool IsDel { get; set; }public static ListUserAppraisalCoefficients ListAll(){ListUserAppraisalCoefficients listnew ListUserAppraisalCoefficients();string sql SELECT ua.*,ap.AppraisalType,ap.AppraisalCoefficient,ap.CalculationMethod FROM UserAppraisals ua LEFT JOIN AppraisalCoefficients ap ON ua.CoefficientIdap.Id;DataTable dtSqlHelper.ExecuteTable(sql);foreach (DataRow dr in dt.Rows){list.Add(dr.DataRowToModelUserAppraisalCoefficients());}return list;}} } 表横向拓展代码以及完整窗体代码如下 using Appraisal_System.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System {public partial class FrmUserAppraisal : Form{public FrmUserAppraisal(){InitializeComponent();}Action bindDgv;private void FrmUserAppraisal_Load(object sender, EventArgs e){SetCol();BindDgvUserAppraisal();//委托bindDgv BindDgvUserAppraisal;}private void BindDgvUserAppraisal(){//get the form which need extendingDataTable dtUser UserAppraisalBases.GetDtJoinAppraisal();//get the coefficient form to extend the main formListAppraisalCoefficients appraisalCoefficients AppraisalCoefficients.ListAll();//fill the dtUser by the coefficient formforeach (var item in appraisalCoefficients){//add the name of coefficientdtUser.Columns.Add(new DataColumn{ColumnName AppraisalType item.Id});//add the value of coefficientdtUser.Columns.Add(new DataColumn{ColumnName AppraisalCoefficient item.Id});//add the calculationMethoddtUser.Columns.Add(new DataColumn{ColumnName CalculationMethod item.Id});}//add the assessmentYeardtUser.Columns.Add(new DataColumn{ColumnName AssessmentYear});//add the YearBonusdtUser.Columns.Add(new DataColumn{ColumnName YearBonus});//fill the dtUserListUserAppraisalCoefficients userAppraisalCoefficients UserAppraisalCoefficients.ListAll();for (int i 0; i dtUser.Rows.Count; i){var uacFilter userAppraisalCoefficients.FindAll(m m.UserId (int)dtUser.Rows[i][Id] m.AssessmentYear Convert.ToInt32(cbxYear.Text));//the array of coefficients to calculationdouble[] yearBonusArray new double[uacFilter.Count];for (int j 0; j uacFilter.Count; j){//get AppraisalType对应的dtUser的ColumnName的值//get Appraisal countstring appraisalTypeKey AppraisalType uacFilter[j].CoefficientId;double appraisalTypeCountValue uacFilter[j].Count;//get Appraisal coefficientstring appraisalCoefficientKey AppraisalCoefficient uacFilter[j].CoefficientId;double appraisalCoefficientValue uacFilter[j].AppraisalCoefficient;//get Appraisal CalculationMethodstring calculationMethodKey CalculationMethod uacFilter[j].CoefficientId;int calculationMethodValue (int)uacFilter[j].CalculationMethod;//bind to dtUserdtUser.Rows[i][appraisalTypeKey] appraisalTypeCountValue;dtUser.Rows[i][appraisalCoefficientKey] appraisalCoefficientValue;dtUser.Rows[i][calculationMethodKey] calculationMethodValue;//calculation the coefficient//count*coefficient*methodyearBonusArray[j] appraisalCoefficientValue * calculationMethodValue * appraisalTypeCountValue;}dtUser.Rows[i][AssessmentYear] cbxYear.Text;//calculation the final bonusdouble yearBonusAll 1;for (int j 0; j yearBonusArray.Length; j){yearBonusAll yearBonusArray[j];}double yearBonusSum yearBonusAll * Convert.ToDouble(dtUser.Rows[i][AppraisalBase]);if (yearBonusSum 0){yearBonusSum 0;}dtUser.Rows[i][YearBonus] yearBonusSum;}dgvUserAppraisal.AutoGenerateColumns false;dgvUserAppraisal.DataSource dtUser;}private void SetCol(){ListAppraisalCoefficients appraisalCoefficients AppraisalCoefficients.ListAll();ListDataGridViewTextBoxColumn dataGridViewTextBoxColumns new ListDataGridViewTextBoxColumn();foreach (var coe in appraisalCoefficients){dataGridViewTextBoxColumns.Add(new DataGridViewTextBoxColumn{HeaderText coe.AppraisalType,Name AppraisalType coe.Id.ToString(),DataPropertyName AppraisalType coe.Id.ToString(),ReadOnly true,Width 88,});dataGridViewTextBoxColumns.Add(new DataGridViewTextBoxColumn{HeaderText 系数,Name AppraisalCoefficient coe.Id.ToString(),DataPropertyName AppraisalCoefficient coe.Id.ToString(),ReadOnly true,Visible false,Width 88,});dataGridViewTextBoxColumns.Add(new DataGridViewTextBoxColumn{HeaderText 计算方式,Name CalculationMethod coe.Id.ToString(),DataPropertyName CalculationMethod coe.Id.ToString(),ReadOnly true,Visible false,Width 88,});}dgvUserAppraisal.Columns.AddRange(dataGridViewTextBoxColumns.ToArray());dgvUserAppraisal.Columns.Add(new DataGridViewTextBoxColumn{HeaderText 考核年度,Name AssessmentYear,DataPropertyName AssessmentYear,ReadOnly true,Width 166});dgvUserAppraisal.Columns.Add(new DataGridViewTextBoxColumn{HeaderText 实发年终奖,Name YearBonus,DataPropertyName YearBonus,ReadOnly true,Width 166});}private void dgvUserAppraisal_MouseDown(object sender, MouseEventArgs e){tsmEdit.Visible false;}private void dgvUserAppraisal_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e){if(e.Button MouseButtons.Right){if(e.RowIndex -1){dgvUserAppraisal.ClearSelection();dgvUserAppraisal.Rows[e.RowIndex].Selected true;tsmEdit.Visible true;}}}private void tsmEdit_Click(object sender, EventArgs e){string yearcbxYear.Text;int userId (int)dgvUserAppraisal.SelectedRows[0].Cells[Id].Value;FrmUserAppraisalEdit frmUserAppraisalEdit new FrmUserAppraisalEdit(userId,year,bindDgv);frmUserAppraisalEdit.ShowDialog();}} }编辑考核项子窗体利用FlowLayoutPanel以及Panel容器动态创造 这个子窗体也用到了委托机制用于再修改后刷新用户考核项情况。 using Appraisal_System.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Appraisal_System {public partial class FrmUserAppraisalEdit : Form{private int _userId;private string _year;private Action _bindDgv;public FrmUserAppraisalEdit(){InitializeComponent();}public FrmUserAppraisalEdit(int userId,string year,Action bindDgv):this(){_userId userId;_year year;_bindDgv bindDgv;}private void btnSave_Click(object sender, EventArgs e){var flCtrs flp.Controls;foreach (Control flCtr in flCtrs){if (flCtr is Panel){var plCtrs flCtr.Controls;foreach (var plCtr in plCtrs){if (plCtr is TextBox){int acId Convert.ToInt32(((TextBox)plCtr).Name.Split(_)[1]);double count Convert.ToDouble(((TextBox)plCtr).Text);UserAppraisals.Delete(_userId, _year, acId);UserAppraisals userAppraisals new UserAppraisals{UserId _userId,CoefficientId acId,AssessmentYear Convert.ToInt32(_year),Count count,IsDel false,};UserAppraisals.Insert(userAppraisals);}}}}MessageBox.Show(修改成功);_bindDgv();this.Close();}private void FrmUserAppraisalEdit_Load(object sender, EventArgs e){CreateContorls();BindControls();}private void BindControls(){ListUserAppraisals userAppraisals UserAppraisals.ListByUserIdAndYear(_userId, _year);foreach (var ua in userAppraisals){var flCtrs flp.Controls;foreach (Control flCtr in flCtrs){if (flCtr is Panel){var plCtrs flCtr.Controls;foreach (var plCtr in plCtrs){if (plCtr is TextBox){int acId Convert.ToInt32(((TextBox)plCtr).Name.Split(_)[1]);if (acId ua.CoefficientId){((TextBox)plCtr).Text ua.Count.ToString();}}}}}}}private void CreateContorls(){ListAppraisalCoefficients appraisalCoefficients AppraisalCoefficients.ListAll();foreach (var ac in appraisalCoefficients){Panel panel new Panel();Label label new Label{Text ac.AppraisalType,Width 60,Location new Point(0, 4),};TextBox txt new TextBox{Location new Point(66, 0),Width 120,Height 26,NametxtAppraisalType_ac.Id,};panel.Controls.Add(label);panel.Controls.Add(txt);flp.Controls.Add(panel);}}private void btnCancel_Click(object sender, EventArgs e){this.Close();}} }
http://www.laogonggong.com/news/123675.html

相关文章:

  • 有口碑的坪山网站建设iis服务器网站301重定向怎么做
  • 营站快车代理平台中山 在门户网站推广
  • 个人备案网站可以做电商吗网站色彩搭配技巧
  • 做品牌特价的网站有哪些郑州网站建设xinsu360
  • 做电影网站需要服务器吗汕头模板自助建站
  • 58网站建设免费网站设计软件
  • 东阳网站建设金山品牌网站建设
  • 可以免费观看电影的网站黄石网站推广排名服务
  • 网站开发前期需要啥阳江网红
  • 商城网站建设要多少钱有什么网站可以做数学题
  • 推广型网站建设公司深夜小网站
  • 做网站优化有用吗做网站的上海公司
  • 网站信息系统建设全球贸易平台
  • 陕西网站建站官网手游
  • 试用网站模版discuz 企业网站 模板
  • 网站开发PHP招聘什么样的网站需要认证
  • 企业网站html百度云求手机网址
  • 开化网站建设开源的公司网站
  • 汕头站扩建招标12306网站开发投资
  • 襄阳网站建设营销学编程的正规网课学校
  • 宜昌住房与城乡建设部网站做个购物商城网站多长时间
  • 哪个网站上做ppt比较好看的企业网站自助建
  • 电商网站 内容优化怎样自做网站
  • 学校资源网站 建设网站维护哪些
  • 创建网站公司 徐州免费发布推广信息的平台
  • 秦皇岛建设厅网站内蒙古自治区建设厅官方网站
  • 做网站的软件名字全拼成都网站开发费用
  • 如何查看网站跳出率做网站如何避免侵权
  • 用python做网站后台企业备案号查询系统
  • wordpress网站好做排名吗国际化的管理咨询公司