我使用以下代码将sql表值加载到数据表中
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Connection String Here");
con.Open();
SqlCommand cmd = new SqlCommand("select * from ExportExcel", con);
dt.Load(cmd.ExecuteReader());
int total = 0;
foreach (DataRow row in dt.Rows)
{
int salaryvalue = Convert.ToInt32(row["Salary"]);
total = salaryvalue + total;
}
dt.Rows.Add("Salary");
dt.Rows.Add(total);
我动态添加了Salary,总计。但是它在第一列中一个接一个地显示。
但是我需要在“部门”栏中输入薪水,在“薪资”栏中输入总计。该怎么做?
您可以使用
dt.rows.add(new object[]{"","Salary",total });
您可以使用计算而不是总计来代替 Datatable.Compute Method
object total;
total= dt.Compute("Sum(Salary)", "");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句