C# WindowsForm 员工管理系统五【查看工资】

来源:互联网 发布:清泉防联云流量软件 编辑:程序博客网 时间:2024/05/16 11:19

考虑到用户分为管理员和普通用户,管理员可以查看所有人的工资记录,而普通用户只能查看自己的工资。

新建ShowPayForm窗体

控件:Label,ComboBox,Button,DataGridView,TextBox
DataGridView绑定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();        }

运行结果:
管理员:这里写图片描述
普通用户:这里写图片描述

0 0
原创粉丝点击