2014年7月13日 星期日

[RESOLVED] How to find the sum of a gridview column in decimal format.


Hi,


I need to select few data from a table in the database and display it in a gridview and need to calculate the sum of a particular column of the gridview and display it in a label. If the column values are 30.30 and 45.30,I am getting the sum as 75.00 and
not as 75.60. How should i change my code to get the sum in decimal form.Below is my code.


cmd.CommandText = "Select XP_ID,XP_NAME,YEAR,MONTH,TEXT,AMOUNT,XRATE,OR_CUR,OR_AMOUNT from Payslip_MISC where XP_ID=" & Session("Employee_ID") & " and Year=" & Session("Year") & " and Month=" & Session("Month") & ""

da.SelectCommand = cmd

da.Fill(dse1, "pay2")

GvDeductions.DataSource = dse1

GvDeductions.DataBind()

Sum_Deductions = 0

 For i = 0 To dse1.Tables("pay2").Rows.Count - 1

    Sum_Deductions += CDec(dse1.Tables("pay2").Rows(i).Item("AMOUNT"))

 Next

lblDeductionsSum.Text = CDec(Sum_Deductions).ToString("#,##0.00")

End If

dse1.Tables("pay2").Clear()

dse1.Clear()


The sum should be displayed in lblDeductionsSum.Text.Some one please help me.


Thanks in advance



I solved it. It was so simple. i declared Sum_Deductions variable as Decimal.It was declared as Integer before.



Hi,


We are happy that your issue has been resolved. And thanks for sharing solution with us.


沒有留言:

張貼留言