C# 对DataTable每一列进行合计,追加到最后一行

原文:https://blog.csdn.net/u011444756/article/details/104941981

/// <summary>
/// DataTable追加合计
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="sumColumns">给哪一列赋值"合计"字符串</param>
/// <returns></returns>
public static DataTable getSumToDatatable(DataTable dt, string sumColumns)
{
string ColumnName = "";
DataRow sum = dt.NewRow();
sum[sumColumns] = "合计";
foreach (DataColumn col in dt.Columns)
{
Type dataType = col.DataType;
ColumnName = col.ColumnName;
if (dataType.Name == "Decimal")
{
sum[ColumnName]= dt.Compute($"sum({ColumnName})", "TRUE");
}
}
dt.Rows.Add(sum);

return dt;

 

posted @ 2020-06-10 10:21  Tozhang  阅读(1167)  评论(0编辑  收藏  举报