您的位置:首页 > 编程语言 > C#

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

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

新建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();
}


运行结果:

管理员:


普通用户:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: