求一个在C#中合计动态列行的值给新创建的列行中显示       ...前面DataSet部分略去不写
        for (int i = 0; i < ms.Length; i++)
        {
            mser = ms[i].Split('#');
            BoundField bf1 = new BoundField();
            bf1.HeaderText = "销量";
            bf1.DataField = "a" + mser[0];
            this.GridView1.Columns.Add(bf2);
        }        BoundField bf3 = new BoundField();
        bf3.HeaderText = "销量合计";
        bf3.DataField = "";        this.GridView1.Columns.Add(bf3);        this.GridView1.DataSource = ds.Tables[0].DefaultView;
        this.GridView1.DataBind();说明:如上代码 "a" + mser[0] 是配合数据库中动态的字段名,现在想把每行的销量值合计显在新创建的列“销量合计”中,(由于某种原因这个合计不能在存储过程里做)请大家出下手

解决方案 »

  1.   

    在datatable中做就可以。ds.Tables[0].Columns.Add("total",typeof(decimal),"sum(销量字段)");
      

  2.   

    绑定total字段就可以了。
      

  3.   

    cpp2017(慕白兄) 
    我的都没有 total字段 呀,如有我就不用提出问题了
      

  4.   

    ds.Tables[0].Columns.Add("total",typeof(decimal),"sum(销量字段)");
    =======>
    我给你的代码就是在datatable中加上这个字段。
      

  5.   

    cpp2017(慕白兄)
    我以下这样加,发现还存在两个问题第一: 我的合计是横着计算的,并非竖着算的,如何横着把各行相加?
    第二: 由于ds.Tables[0].Columns ...这句是放在for 里,如两上以上循环时会报 “名为"tatal"的列已属于此DateTable” 的错,如何避免?
            for (int i = 0; i < ms.Length; i++)
            {
                mser = ms[i].Split('#');
                BoundField bf1 = new BoundField();
                bf1.HeaderText = "销量";
                bf1.DataField = "a" + mser[0];
                ds.Tables[0].Columns.Add("total",typeof(decimal),"a" + mser[0]);
                this.GridView1.Columns.Add(bf2);
            }        BoundField bf3 = new BoundField();
            bf3.HeaderText = "销量合计";
            bf3.DataField = "tatal";
      

  6.   

    参考:http://www.google.com/search?hl=zh-CN&newwindow=1&rls=com.microsoft%3Aen-US&q=gridview+ListItemType.Footer+%E5%90%88%E8%AE%A1&lr=lang_zh-CN%7Clang_zh-TW
      

  7.   

    哦,上面我搜索的不对。行的合计你写个函数从ds.Tables[0]里计算不行吗?
      

  8.   

    cpp2017(慕白兄) & sp1234(想象力真的那么难吗?) 再指点下,就差我上面提到的二点想不通了
      

  9.   

    if(e.Item.ItemType==ListItemType.footer)
    {
        int per = Convert.ToInt32(e.Item.Cells[0].Text);
        sum+=per;
         e.item.cells[0].Text=sum;
    }
      

  10.   

    wangdetian168你这样取值勤,int per = Convert.ToInt32(e.Item.Cells[0].Text); 不行的,我的Cells[0]列值还没生成呀,似乎你的答案占不了边
      

  11.   

    sorry 
    上面写错了应该是
    ds.Tables[0].Columns.Add("total",typeof(decimal),"字段1+字段2");
      

  12.   

    非常感谢cpp2017(慕白兄),这问题折了我一下午,最后的一点让我矛塞盾开,问题得到解决