如果更改datagridview中的行,如何更新标签的文本?

时间:2012-08-23 00:58:20

标签: vb.net datagridview

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

1 个答案:

答案 0 :(得分:0)

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

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

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

End Sub