2014-04-01 28 views
0

數據樣本:20.50和20.00 合計:41.00DataGridView的總和列錯誤/ vb.net

我怎樣才能得到確切數量,因爲40.50?我可以對列進行求和並顯示標籤,但是我無法解決舍入問題。

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 

回答

0

我假設變量totalInteger,試着改變你的total變量輸入Double

某處你的代碼..

Dim total As Double = 0 

,然後改變這一點:

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 

,如果你想要做一些四捨五入,按照這個link

+0

我的錯。沒有這樣的東西'tutar'。這是'全部'。 '如果DataGridView1.RowCount> 1然後 昏暗總作爲雙人= 0 對於索引爲整數= 0要DataGridView1.RowCount - 1個 總+ = Convert.ToDouble(DataGridView1.Rows(指數).Cells(3)。 Value) Next Label1.Text = total.ToString() End If' 我試過了,並沒有工作。 – burak

+0

沒關係。只要將'total'變成'double',或者你也可以用'decimal' ..根據你的需要。 – Codemunkeee

+0

試過兩次或十進制。兩者都不起作用。圍繞結果四捨五入。 – burak