C# WindowsForm 员工管理系统五【查看工资】
2015-09-28 17:27
861 查看
考虑到用户分为管理员和普通用户,管理员可以查看所有人的工资记录,而普通用户只能查看自己的工资。
DataGridView绑定StaffInfo表,readonly=true
编辑ComboBox,输入StaffInfo.Office属性
在窗体加载时应该对当前用户的类型进行识别,如果是普通用户,则禁用分类搜索功能且之读取该用户的工资信息
private static int id = Form1.ID;
private string SelectValue=”“;
运行时会发现DataGridView最后会有一空白行,所以遍历它时应少一次,否则其cell[]==Null,出现NullReferenceException
添加ComboBox的SelectedValueChanged事件
方法:选中ComboBox控件,点击右下角属性右边的事件图标,双击SelectedValueChanged
双击“确认” 按钮
双击“返回”按钮
最后将ShowPayForm窗体与主窗体关联起来,双击主窗体的“查看工资”按钮
运行结果:
管理员:
普通用户:
新建ShowPayForm窗体
控件:Label,ComboBox,Button,DataGridView,TextBoxDataGridView绑定StaffInfo表,readonly=true
编辑ComboBox,输入StaffInfo.Office属性
在窗体加载时应该对当前用户的类型进行识别,如果是普通用户,则禁用分类搜索功能且之读取该用户的工资信息
private static int id = Form1.ID;
private string SelectValue=”“;
private void ShowPayForm_Load(object sender, EventArgs e) { if (Form1.UserType.Trim() == "Administrator") { // TODO: 这行代码将数据加载到表“staffDataSet.Pay”中。您可以根据需要移动或删除它。 this.payTableAdapter.Fill(this.staffDataSet.Pay); } else { string sql = "select*from Pay where ID=" + id + ""; string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand(sql, conn); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); dataGridView1.DataSource = dt; comboBox1.Enabled = false; btnOk.Enabled = false; } display(); }
运行时会发现DataGridView最后会有一空白行,所以遍历它时应少一次,否则其cell[]==Null,出现NullReferenceException
private void display() { int totalMoney = 0; int totalMan = 0; int Id; List<int> listID = new List<int>(); int count = dataGridView1.Rows.Count; foreach (DataGridViewRow row in dataGridView1.Rows) { count--; if (count > 0) { Id = Convert.ToInt32(row.Cells[0].Value.ToString()); if (!listID.Contains(Id)) { listID.Add(Id); totalMan++; } totalMoney += Convert.ToInt32(row.Cells[6].Value.ToString()); } } TotalMan.Text = totalMan.ToString(); TotalMoney.Text = totalMoney.ToString(); }
添加ComboBox的SelectedValueChanged事件
方法:选中ComboBox控件,点击右下角属性右边的事件图标,双击SelectedValueChanged
private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { SelectValue = comboBox1.Text; }
双击“确认” 按钮
private void button1_Click(object sender, EventArgs e) { string sql = ""; if (SelectValue == "") { sql = "select*from Pay"; } else { string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(connstr); sql = "select * from Pay where Pay.ID in(select ID from StaffInfo where StaffInfo.Office='" + SelectValue + "')"; SqlCommand cmd = new SqlCommand(sql, conn); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); dataGridView1.DataSource = dt; } display(); }
双击“返回”按钮
this.Close();
最后将ShowPayForm窗体与主窗体关联起来,双击主窗体的“查看工资”按钮
private void toolStripButton3_Click(object sender, EventArgs e) { ShowPayForm ShowpayForm = new ShowPayForm(); ShowpayForm.Show(); }
运行结果:
管理员:
普通用户:
相关文章推荐
- C#结构体特性实例分析
- c#中字符串截取使用的方法(转)
- C#线程编程实例
- 判断多边形是顺时针还是逆时针(C#实现)
- C#.NET实现网页自动登录的方法
- C#中的面向对象编程: 类和对象
- C#多线程编程(转载)
- c#中如何避免Button,CheckBox等的点击状态时的焦点矩形框问题:
- C# .NET 发送邮件 代码
- C#实现二叉树遍历算法
- C# form 传参数的几个方法
- C#中form窗体之间的传递几个方法学习
- C#与闭包(closure)学习笔记
- C#与重构(入门)
- 基于C#实现简单离线注册码生成与验证
- C#委托(delegate、Action、Func、predicate)
- c# 使用protobuf
- C#开发中遇到问题常问题
- 控制台I/O
- C#中的MessageBox的show的各种重载方法使用