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

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

新建ShowPayForm窗体

控件:Label,ComboBox,Button,DataGridView,TextBox
DataGridView绑定StaffInfo表,readonly=true

C# WindowsForm 员工管理系统五【查看工资】_第1张图片

编辑ComboBox,输入StaffInfo.Office属性

C# WindowsForm 员工管理系统五【查看工资】_第2张图片

在窗体加载时应该对当前用户的类型进行识别,如果是普通用户,则禁用分类搜索功能且之读取该用户的工资信息

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 listID = new List();
            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# WindowsForm 员工管理系统五【查看工资】_第3张图片
普通用户:C# WindowsForm 员工管理系统五【查看工资】_第4张图片

你可能感兴趣的:(C#,WinForm,管理,员工)