zoukankan      html  css  js  c++  java
  • DataTable添加统计行及统计列实例

                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;
                }
  • 相关阅读:
    get pc name in LAN
    study spring
    android install
    【转】Java:Session详解
    classic problem: producer and consumer
    tomcat install
    验证Monitor.PulseAll功效
    (转)Windows7的图形架构与DX的那点事
    Cannot open your default email folders. The attempt to log on to Microsoft Exchange has failed.
    Monitor.Wait初探(8)
  • 原文地址:https://www.cnblogs.com/captainR/p/2644284.html
Copyright © 2011-2022 走看看