如何在gridview列上循环,将其值相加并在新行上显示总和? [英] How do I loop on column of gridview, sum its values and dsplay the sum on a new line?
问题描述
正如问题所说,我正在尝试将网格视图中列的总和显示给网格外的用户,但是我在下面编写的代码中出现错误。
所以我可以我更正了代码,以便显示总和?
获得此错误:
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 :
Operator '+' is not defined for types 'Double' and 'System.Web.UI.WebControls.TableCell
我尝试过:
What I have tried:
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
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)
推荐答案
错误给你一个巨大的线索!
你知道总和
是一个Double,你知道你得到的错误
The error is giving you a huge clue!
You know thatSum
is a Double and you know you are getting the error on
Sum = Grid1.Rows(i).Cells(3) + Sum
和/或
Sum = Sum + (row.Cells(3))
所以按理说它是 Grid1.Rows(i).Cells(3)
和/或 row.Cells(3)
这是一个'System.Web.UI.WebControls.TableCell'
所以你需要将Cell转换为Double才能将它添加到运行总计总和
例如
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.
Sum = Double.Parse(Grid1.Rows(i).Cells(3).Text) + Sum
或
or
Sum = Sum + Double.Parse(row.Cells(3).Text)
这篇关于如何在gridview列上循环,将其值相加并在新行上显示总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!