这个你最好在sql中就组织好,要不就别直接挂数据集,遍历数据集,填充到grid,这样可以自己统计合计,增加行列,控制比较简单

解决方案 »

  1.   

    你可以使用DevExpress中的GridControl,其有一个SummaryItems可以控制,单纯的使用自带的DataGridView就需要自己弄代码了,版主bdmh提供的思路就不错
      

  2.   

    Sql中:
    string Sql = "select dwmc as 单位名称,count(case when t.zl in 'A' then 'A' end) A类卡,count(case when t.zl in 'B' then 'B' end) B类卡,count(case when t.zl in 'C' then 'C' end) C类卡,count(case when t.zl in 'D' then 'D' end) D类卡,count(case when t.zl in 'E' then 'E' end) E类卡,count(case when t.zl in 'F' then 'F' end) F类卡,count(case when t.zl in 'T' then 'T' end) T类卡 from T_TRUCK t where 1=1";                string KZT_First;  //定义将要截取的卡状态首字母为字符串类型
                    KZT_First = comboBox1.Text.Trim().Substring(0, 1);//截取卡状态文本框的首字母
                    Sql += "and t.gsbz = '" + KZT_First + "'";//查询选中的卡状态的数据,卡状态为必选项                //查询选中的单位的数据,为非必选项
                    if (comboBox4.Text != "")
                    {
                        if (comboBox3.Text == "")
                        {
                            Sql += "and t.dwdm like '" + comboBox4.Text + "%'";
                        }
                        if (comboBox3.Text != "")
                        {
                            Sql += "and t.dwdm = '" + comboBox4.Text + "'";
                        }                }
                    if (textBox1.Text != "")
                    {
                        if (textBox2.Text == "")
                        {
                            Sql += " and To_Char(t.bkq,'yyyy/MM/dd') = '" + textBox1.Text + "'";
                        }
                        if (textBox2.Text != "")
                        {
                            Sql += " and To_Char(t.bkq,'yyyy/MM/dd') between '" + textBox1.Text + "' and '" + textBox2.Text + "'";
                        }
                    }
    Sql += "group by dwmc order by dwmc";//数据分组及排序
    我觉得已经很复杂了,再如何添加呢?还有您说的第二种遍历数据集如何实现的呢?我刚开始做编程实在不懂,能不能给个代码?谢谢了!
      

  3.   

    这个你就要 修改 dt 了 
    个dt 新加 一列 或 一行 public static System.Data.DataTable ColumnsAdd(Query_Cost_FeeInfo cfi)
            {
                System.Data.DataTable dt = Cost_FeeInfoManager.GetBillNumberInfo(cfi);
                if (dt != null)
                {
                    DataColumn dtColumnTotal = new DataColumn("合计", typeof(decimal));
                    dt.Columns.Add(dtColumnTotal);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        decimal money = 0;
                        //从第九列开始合计,因为前几列都是 字符类型 不需要合计,我这个是 行转列,并且把要合计的列 固定在第九列了
                        for (int j = 9; j < dt.Columns.Count; j++)
                        {
                            if (dt.Columns[j].ColumnName != "合计")
                            {
                                if (dt.Rows[i][j] != DBNull.Value)
                                    money += Convert.ToDecimal(dt.Rows[i][j]);
                                dt.Rows[i]["合计"] = money;
                            }
                        }
                    }
                    DataRow drNew = dt.NewRow();
                    dt.Rows.Add(drNew);
                    for (int i = 9; i < dt.Columns.Count; i++)
                    {
                        if (dt.Columns[i].ColumnName == "合计")
                        {
                            decimal money = 0;
                            for (int row = 0; row < dt.Rows.Count; row++)
                            {
                                if (dt.Rows[row][i] != DBNull.Value)
                                {
                                    money += Convert.ToDecimal(dt.Rows[row][i]);
                                }                        }
                            dt.Rows[dt.Rows.Count - 1][i] = money;
                        }
                        dt.Rows[dt.Rows.Count - 1]["经营单位"] = "总计";
                    }
                }
                return dt;
            }