简体   繁体   中英

Datagridview Sum Column Error / vb.net

Data sample: 20.50 and 20.00 Total: 41.00

How can i get the exact number as 40.50 ? I can sum the column and show in label, however i can not solve the rounding problem.

If DataGridView1.RowCount > 1 Then
        Dim tutar As Integer = 0
        For index As Integer = 0 To DataGridView1.RowCount - 1
            total += Convert.ToInt32(DataGridView1.Rows(index).Cells(3).Value)
        Next
        Label1.Text = total.ToString("N2")
    End If

I assume that the variable total is an Integer , try changing your total variable to type Double .

somewhere in your code..

Dim total As Double = 0

and then change this:

If DataGridView1.RowCount > 1 Then
    Dim tutar As Integer = 0
    For index As Integer = 0 To DataGridView1.RowCount - 1
        total += Convert.ToDouble(DataGridView1.Rows(index).Cells(3).Value.tostring)
    Next
    Label1.Text = total.ToString()
End If

and if you want to do some rounding, follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM