请教一下,我为业务部门制作的这个月度考核统计表,查询后按班组分类并统计合计,现在需要按不同的产品类型再区分显示,但这样的话,那些不符条件,不能显示的查询内容的合计还在,请问如何能灵活的,按条件来显示内容与合计行呢,截图与代码如下,敬请指导,谢谢;
string startdate = comdate.Text;
//---------------------------------------0716,以下,是为了区分制剂一和二所加;
string comtype = comType.Text;
string teamsFilter = "";
if (comtype == "制剂一")
{
teamsFilter = "teams <> '液体制剂组' AND teams <> '液体包装组'";
}
else if (comtype == "制剂二")
{
teamsFilter = "teams <> '片剂外包组' AND teams <> '颗粒制剂组' AND teams <> '片剂制剂组'";
}
//Sqlstring = @"select * from w_TCSalary where dateInfo = @startdate order by departMent ,teams";
Sqlstring = @"select * from w_TCSalary where dateInfo = @startdate";
if (!string.IsNullOrEmpty(teamsFilter))
{
Sqlstring += " AND " + teamsFilter;
}
Sqlstring += " order by departMent , teams"; // 0716 , +=号,是为了追加后面的查询条件;
//-----------------------------------------------------
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);
//-------------------------------------------------------------------------- 0710 按班组分类;
DataTable dtTeams = new DataTable();
string sqlstring = $"select distinct teams from w_TCSalary where dateInfo='{startdate}' order by teams asc";
Cmd = new SqlCommand(sqlstring, Conn);
sda = new SqlDataAdapter(Cmd);
sda.Fill(dtTeams);
dtSum = dt.Clone();
dtSum.Clear();
// 计算各班组合计值并插入到原始数据表中
foreach (DataRow teamRow in dtTeams.Rows)
{
//---------------------0716,按制剂类型分类
string teamName = teamRow["teams"].ToString();
// 筛选出当前班组的数据行
DataRow[] rows = dt.Select($"teams = '{teamName}'");
//------------------------------------------
// 计算该班组的合计值
decimal sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0;
foreach (DataRow row in rows)
{
sum1 += Convert.ToDecimal(row["workday"]);
sum2 += Convert.ToDecimal(row["Smanage"]);
sum3 += Convert.ToDecimal(row["reward"]);
sum4 += Convert.ToDecimal(row["pamount"]);
sum5 += Convert.ToDecimal(row["Salary"]);
// 将数据行添加到结果表中
dtSum.ImportRow(row);
}
// 创建合计行并插入到数据表中
DataRow totalRow = dtSum.NewRow();
totalRow["teams"] = "合计";
totalRow["workday"] = sum1;
totalRow["Smanage"] = sum2;
totalRow["reward"] = sum3;
totalRow["pamount"] = sum4;
totalRow["Salary"] = sum5;
dtSum.Rows.Add(totalRow);
}