Gridview 列和行总计

发布于 2024-10-18 15:06:52 字数 557 浏览 3 评论 0原文

我的 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#。你能让我知道我该怎么做吗?

是不是这样,我必须按行和列解析网格,然后执行 SUM?像下面

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

有没有更好的 wau 来实现这个目标?多谢。

谢谢, 拉胡尔

my gridview has data which looks like below (for example)

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

What I am trying to do is ... performing a sum by Row and Column and add another column to grid ... So that ultimately it will looks like below

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

17      28     24

Is it pssible in C#. Can you please let me know how can I do this.

is it like, I have to parse through the grid by Row and Column and then perform the SUM; like below

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

Is there any better wau to achive this? Thanks a lot.

Thanks,
Rahul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-10-25 15:06:52

好吧,我终于得到了它,所以,考虑将其发布......如果它对其他人有帮助的话。

我没有处理来自网格的数据...我将 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());
         }
     }

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

Well I have got it finally and So, thought of posting it ... if it helps others.

Instead of processing the data from grid ... I put the logic for Sum in my processing and then bind that data to grid ... which worked.

Can be done in gridview as well ... we have to put the processing in "gridview_onrowdatabound" event as below

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());
         }
     }

Hope this Helps.
Thanks,
Rahul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文