Gridview column and row total

自作多情 提交于 2019-12-11 04:09:53

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5098793/gridview-column-and-row-total

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!