1 Previous Next 

CurrencyManager a simple sample


Show synchronise data in labels based on the grid or on the buttons It is very simple.
Take a look at this sample. It needs only a Form Project with dragged on that a DataGridView (can be left out), two buttons and four labels .
You can paste the code below into the code view and click the green start button in top (or F5)

(Be aware it is done with VB10SP1 for older you have to add on some places byval and then it goes even in VB2002)
Public Class Form1
    Private dt As New DataTable
    Private cma As CurrencyManager = DirectCast(BindingContext(dt), CurrencyManager)
    Private Sub Form1_Load(sender As Object, e As System.EventArgsHandles MyBase.Load
        Button1.Text = "Forward"
        Button2.Text = "Back"
 
        dt.Columns.Add("Name")
        dt.Columns.Add("State")
        dt.Columns.Add("Nation")
        dt.LoadDataRow(New Object() {"Ken Tucker""Florida""USA"}, True)
        dt.LoadDataRow(New Object() {"Cor Ligthert""Netherlands""EU"}, True)
        dt.LoadDataRow(New Object() {"John Antonny Oliver""England""EU"}, True)
        dt.LoadDataRow(New Object() {"Armin Zignler""Germany""EU"}, True)
        dt.LoadDataRow(New Object() {"Hannes Heslacher""Germany""EU"}, True)
        dt.LoadDataRow(New Object() {"Paul Clement""Illinois""USA"}, True)
        dt.LoadDataRow(New Object() {"Mark Liu""SjangHai""China"}, True)
        dt.LoadDataRow(New Object() {"Mike Feng""SjangHai""China"}, True)
        DataGridView1.DataSource = dt
        Label1.DataBindings.Add("Text", dt, "Name")
        Label2.DataBindings.Add("Text", dt, "State")
        Label3.DataBindings.Add("Text", dt, "Nation")
        Label4.Text = "0"
        AddHandler cma.CurrentChanged, AddressOf CurrentChanged
    End Sub
    Private Sub CurrentChanged(ByVal sender As ObjectByVal e As EventArgs)
        Label4.Text = DirectCast(sender, CurrencyManager).Position.ToString
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        If cma.Position < dt.Rows.Count + 2 Then
            cma.Position += 1
        End If
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgsHandles Button2.Click
        If cma.Position > 0 Then
            cma.Position -= 1
        End If
    End Sub
End Class




1 Previous Next