1 Previous Next 

CurrencyManager a simple sample


Synchronise labels (or whatever) with a DataGridView (actually the datasource) is very simple.
Take a look at this sample. It needs only a Form Project with dragged on that a DataGridView and three labels.
You can paste the code below into the code view and click the green start button in top (or F5)

Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add("Name")
        dt.Columns.Add("State")
        dt.LoadDataRow(New Object() {"Ken", "Florida"}, True)
        dt.LoadDataRow(New Object() {"Cor", "Holland"}, True)
        DataGridView1.DataSource = dt
        Label1.DataBindings.Add("Text", dt, "Name")
        Label2.DataBindings.Add("Text", dt, "State")
        Label2.Text = "1"
        Dim cma As CurrencyManager = DirectCast(BindingContext(dt), CurrencyManager)
        AddHandler cma.CurrentChanged, AddressOf CurrentChanged
      End Sub
      Private Sub CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
         Label3.Text = DirectCast(sender, CurrencyManager).Position.ToString
      End Sub
End Class



1 Previous Next