各位好,我查询数据表,查询的同时完成针对工资Salary的基础运算,并保存到数据表里面,如下图的search事件里面,然后再在CellEndEdit,完成工资Salary + 奖励reward - 扣罚PAmount,,但在运行时,计算的结果总不正确,计算前如第一图0700,计算后如第二图0701,请给排查一下,谢谢;
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow currentRow = dataGridView1.Rows[e.RowIndex];
if (e.ColumnIndex == 8 || e.ColumnIndex == 9 || e.ColumnIndex == 10)
{
decimal salary = Convert.ToDecimal(currentRow.Cells[10].Value);
decimal pAmount = Convert.ToDecimal(currentRow.Cells[9].Value);
decimal reward = Convert.ToDecimal(currentRow.Cells[8].Value);
decimal result = salary + reward - pAmount;
currentRow.Cells[10].Value = result.ToString();
}
}
private void btnsearch_Click(object sender, EventArgs e)
{
using (SqlConnection Conn = new SqlConnection("server=192.168.100.247;database=Whmesinfo;User=Sa;Password=whyy@2021"))
{
Conn.Open();
if ((comdate.Text == "请选择") || (string.IsNullOrEmpty( txtstandard.Text)))
{
MessageBox.Show("请您选择查询月份,并填入标准出勤天数");
return;
}
else
{
try
{
string startdate = comdate.Text;
Sqlstring = @"select * from w_TCSalary where dateInfo = @startdate";
SqlCommand Cmd = new SqlCommand(Sqlstring, Conn);
Cmd.Parameters.AddWithValue("@startdate", startdate);
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
dt.Rows.Clear();
dt.Columns.Clear();
dataGridView1.Columns.Clear();
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderText = "日期";
dataGridView1.Columns[2].HeaderText = "姓名";
dataGridView1.Columns[3].HeaderText = "部门";
dataGridView1.Columns[4].HeaderText = "班组";
dataGridView1.Columns[5].HeaderText = "职位";
dataGridView1.Columns[6].HeaderText = "出勤天数";
dataGridView1.Columns[7].HeaderText = "现场管理";
dataGridView1.Columns[8].HeaderText = "奖励金额";
dataGridView1.Columns[9].HeaderText = "扣罚金额";
dataGridView1.Columns[10].HeaderText = "发放金额";
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//点击全选操作
foreach (DataRow row in dt.Rows)
{
decimal workday = Convert.ToDecimal(row["workday"]);
decimal Smanage = Convert.ToDecimal(row["Smanage"]);
decimal Reward = Convert.ToDecimal(row["reward"]);
decimal Pamount = Convert.ToDecimal(row["pamount"]);
decimal Salary = Convert.ToDecimal(row["Salary"]);
decimal workstandard = string.IsNullOrWhiteSpace(txtstandard.Text) ? 0 : Convert.ToDecimal(txtstandard.Text);
decimal Temp;
if (workday / 1 > workstandard)
{
Temp = 50m;
}
else
{
Temp = Math.Round(workday / workstandard * 50, 2, MidpointRounding.AwayFromZero);
}
Salary = Smanage + Temp + Reward - Pamount;
row["Salary"] = Salary;
}
//完成运算后,随即对数据表执行更新操作;
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Update(dt);
dataGridView1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}