在我的表單上我有一個標籤(lblBalance),我有一個從Access數據庫填充的DGV。如果我添加,刪除或更新一行,如何刷新標籤文本以反映新的餘額?如何在datagridview中的某行更改時更新標籤的文本?
這是我frmMain代碼至今:
Public Class frmMain
Private Sub TransactionsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TransactionsBindingNavigatorSaveItem.Click
Me.Validate()
Me.TransactionsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CheckingDataSet)
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CheckingDataSet.Transactions' table. You can move, or remove it, as needed.
Me.TransactionsTableAdapter.Fill(Me.CheckingDataSet.Transactions)
lblTotalCredits.Text = BalanceTotal().ToString("c")
End Sub
Private Function BalanceTotal() As Double
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To TransactionsDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(TransactionsDataGridView.Rows(i).Cells(3).Value)
Next i
Return tot
End Function
End Class
如果您在處理金錢問題,您應該考慮使用Decimal類型而不是Double類型。 – LarsTech
使用DGV事件,比如'DGV.RowsRemoved' – UnLoCo