2014-04-23 73 views
0

我想計算gridview行的值,並希望顯示在文本框中。 我有5行gridview,itemid,itemname,orderedqty,price,totalprice。當我點擊添加行時,我想計算總價格總額。想要顯示它到文本框。如何計算gridview行並顯示答案到文本框

我在想這是代碼,我認爲它不起作用。請幫我找到解決辦法。

Public Sub Calculate() 
    Dim Countrow As Integer = 1 
    Dim AmountTotal As Decimal = 0 

    For Each row As DataGridView In dgvPurchaseOrder.Rows 
     Countrow += 1 
     AmountTotal = dgvPurchaseOrder.Rows.Count.ToString() 
    Next 
    'GridView1.FooterRow.Cells(1).Text = "Total = " & AmountTotal.ToString() 
    txtGrossTotal.Text = AmountTotal.ToString() 
End Sub 
+0

你在計算什麼? –

+0

計算總價格行。 – NAJEEB

+0

在這一行顯示錯誤-----對於每行DataGridView在dgvPurchaseOrder.Rows中------------------------------ ---- (無法強制類型爲「System.Windows.Forms.DataGridViewRow」的對象鍵入'System.Windows.Forms.DataGridView'。) – NAJEEB

回答

0

嘗試像這樣

Public Sub Calculate() 
     Dim Countrow As Integer = 1 
     Dim AmountTotal As Decimal = 0 

     For Each row As DataGridView In dgvPurchaseOrder.Rows 
      Countrow += 1 
      AmountTotal += row.Cells(4).Value // give correct price column index 
     Next 
       txtGrossTotal.Text = AmountTotal.ToString() 
    End Sub 
+0

amounttotal + = row.cells(4).value此行不接受。「單元格不是system.windows.forms的成員。 datagridview「 – NAJEEB

+0

我不知道它不接受,那一行....請幫助我。 – NAJEEB

+0

@NAJEEB使用'AmountTotal + = row.Cells(4).Value.ToDecimal()' – equisde

0
Public Sub Calculate() 
    Dim Countrow As Integer = 1 
    Dim AmountTotal As Decimal = 0 

    For Each row As DataGridViewRow In dgvPurchaseOrder.Rows 
     Countrow += 1 
     AmountTotal += row.Cells(4).Value 
    Next 
    txtGrossTotal.Text = AmountTotal.ToString() 
End Sub 

@equisde你是英雄,這是正確的密碼。

+0

感謝@SATSON。他給出了答案。我剛糾正了一個錯誤。請隨時接受SATSON的答覆。 – equisde

+0

是的,他是創造的,非常感謝謝謝... – NAJEEB

相關問題