0

在我的表单上,我有一个标签 (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
4

1 回答 1

0

BindingSource 控件有一个ListChanged事件,您可以尝试使用它来更新余额标签。

Private Sub TransactionsBindingSource_ListChanged(_
                sender As Object, _
                e As ListChangedEventArgs) _
                Handles TransactionsBindingSource.ListChanged

  lblTotalCredits.Text = BalanceTotal().ToString("c")

End Sub
于 2012-08-23T01:04:16.157 回答