Rahul
Rahul

Reputation: 77886

Gridview column and row total

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

Upvotes: 1

Views: 6136

Answers (1)

Rahul
Rahul

Reputation: 77886

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

Upvotes: 0

Related Questions