原文: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;