DataSet ds = dbSys.ExecuteDataSet(QueryString());
DataTable dt = ds.Tables[0];
//添加统计行
DataRow newRow = dt.NewRow();
newRow[dt.Columns[1].ColumnName] = "Grand Total";
double total = 0;
string totalName = string.Empty;
for (int i = 2; i < dt.Columns.Count; i++)
{
total = 0; //换列统计时先清空
Type type = dt.Columns[i].DataType; //获取列数据的类型
if (!string.IsNullOrEmpty(dt.Columns[i].ColumnName) && dt.Columns[i].DataType.Name == "String")
{
for (int j = 0; j < dt.Rows.Count; j++)
{
if (!string.IsNullOrEmpty(dt.Rows[j][dt.Columns[i].ColumnName].ToString()))
{
total += Convert.ToDouble(dt.Rows[j][dt.Columns[i].ColumnName]);
}
}
totalName = ds.Tables[0].Columns[i].ColumnName;
}
newRow[totalName] = total;
}
dt.Rows.Add(newRow);
//添加统计列
total = 0; //清空赋值
totalName = string.Empty; //清空赋值
DataColumn col = dt.Columns.Add("Grand Total", typeof(string));
for (int i = 0; i < dt.Rows.Count; i++)
{
total = 0;
for (int j = 2; j < dt.Columns.Count; j++)
{
if (!string.IsNullOrEmpty(dt.Rows[i][dt.Columns[j].ColumnName].ToString()))
{
total += Convert.ToDouble(dt.Rows[i][dt.Columns[j].ColumnName]);
}
}
totalName = total.ToString();
dt.Rows[i]["Grand Total"] = totalName;
}