在我的表单上,我有一个标签(lblBalance),我有一个从Access数据库填充的DGV。如果我添加,删除或更新行,如何刷新标签文本以反映新的余额?
到目前为止,这是我的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
答案 0 :(得分:0)
BindingSource控件有一个ListChanged
事件,您可以尝试使用它来更新余额标签。
Private Sub TransactionsBindingSource_ListChanged(_
sender As Object, _
e As ListChangedEventArgs) _
Handles TransactionsBindingSource.ListChanged
lblTotalCredits.Text = BalanceTotal().ToString("c")
End Sub