Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As the question says,I am trying to Display the sum of a column in gridview to the user outside the grid,but i am getting errors with the code i wrote below.
So ho can i correct the code in order for it to display the Sum?

P.s getting this Error :
C#
Operator '+' is not defined for types 'Double' and 'System.Web.UI.WebControls.TableCell


What I have tried:

VB
Dim Sum As Double = 0
       For i As Integer = 0 To Grid1.Rows.Count - 1
           Sum = Grid1.Rows(i).Cells(3) + Sum
       Next
       Response.Write("The Sum of all RepairPrice is : " + Sum.ToString)

I also tried this
VB
Dim Sum As Double = 0
       For Each row In Grid1.Rows

           Sum = Sum + (row.Cells(3))

       Next
       Console.WriteLine("The Sum of all RepairPrice is : " + Sum.ToString)
Posted
Updated 28-Jun-16 5:45am
v2

1 solution

The error is giving you a huge clue!

You know that Sum is a Double and you know you are getting the error on
VB
Sum = Grid1.Rows(i).Cells(3) + Sum
and/or
VB
Sum = Sum + (row.Cells(3))
so it stands to reason that it's Grid1.Rows(i).Cells(3)and/or row.Cells(3) that is a 'System.Web.UI.WebControls.TableCell'

So you need to convert the Cell to a Double in order to add it to the running total Sum
E.g.
VB
Sum = Double.Parse(Grid1.Rows(i).Cells(3).Text) + Sum
or
VB
Sum = Sum + Double.Parse(row.Cells(3).Text)
 
Share this answer
 
Comments
xTMx9 29-Jun-16 8:37am    
Can you help me with my question here please: http://www.codeproject.com/Questions/1109458/How-can-I-move-forward-and-backward-through-rows-o

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900