【问题标题】:Gridview column and row totalGridview 列和行总计
【发布时间】:2011-02-25 07:05:24
【问题描述】:

我的 gridview 有如下所示的数据(例如)

col1   col2   col3
2      10      1
4       3      5
11      15    18

我想要做的是...按行和列执行求和,然后将另一列添加到网格中...这样最终它会如下所示

col1   col2   col3  Total
2      10      1    13
4       3      5    12
11      15    18    44

17      28     24

在 C# 中是否可行。请告诉我该怎么做。

是这样吗,我必须按行和列解析网格,然后执行求和;如下所示

foreach (gridviewrow row in gridview1.rows)
{
  add the value for cell 0;
  cell 1;
  cell 2;
 }

有没有更好的 wau 来实现这一点?非常感谢。

谢谢, 拉胡尔

【问题讨论】:

  • 实际数据从何而来?

标签: gridview c#-2.0


【解决方案1】:

嗯,我终于明白了,所以,想把它贴出来……如果它可以帮助别人。

而不是处理来自网格的数据......我将 Sum 的逻辑放在我的处理中,然后将该数据绑定到网格......这很有效。

也可以在gridview中完成...我们必须将处理放在“gridview_onrowdatabound”事件中,如下所示

protected void gridview1_onrowdatabound(Object sender, GridViewRowEventArgs e)
{
 // Check if it's a header row
  if (e.Row.RowType == DataControlRowType.Header) 
   {
      e.Row.Cells.add("Total"); //add a header Col
    }

     int count = 0;

     if(e.Row.RowType == DataControlRowType.DataRow)
        {
            count = count + Convert.ToInt32(e.Row.Cells[0].value)+cell1 value+cell2 value;
          e.Row.Cells.add(count.tostring());
         }
     }

希望这会有所帮助。 谢谢, 拉胡尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多