Public Class frmDataBindings
'Datenquelle
Dim dt As New DataTable
Dim WithEvents bs As New BindingSource
'Datenbindung / Controls
Dim lblSpalte0 As New Label With _
{.Parent = Me, .Width = 100}
Dim WithEvents txtSpalte1 As New TextBox _
With {.Parent = Me, .Top = 20, .Width = 100}
Dim WithEvents dtpSpalte2 As New DateTimePicker _
With {.Parent = Me, .Top = 40}
'Navigation in Zeilen
Dim WithEvents btnNext As New Button With _
{.Parent = Me, .Top = 80, .Width = 100, .Text = "Next"}
Dim WithEvents btnPrevious As New Button With _
{.Parent = Me, .Top = 80, .Width = 100, .Left = 110, .Text = "Prev."}
'Zeile anfügen
Dim WithEvents btnAdd As New Button With _
{.Parent = Me, .Top = 110, .Width = 100, .Text = "Add"}
'XML speichern
Dim WithEvents btnSave As New Button With _
{.Parent = Me, .Top = 130, .Width = 100, .Text = "Save"}
Private Sub frmDataBindings_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
'Testdaten
With dt
With .Columns
.Add("ID", GetType(Integer))
.Add("TextSpalte", GetType(String))
.Add("DateSpalte", GetType(Date))
End With
.TableName = "MyTable"
.PrimaryKey = {dt.Columns(0)}
.Columns(0).AllowDBNull = False
.Columns(0).Unique = True
With .Rows
.Add(1, "Zeile1", New Date(2017, 5, 21))
.Add(2, "Zeile2", New Date(2017, 6, 11))
.Add(3, "Zeile3", New Date(2017, 7, 25))
.Add(4, "Zeile4", New Date(2017, 8, 4))
End With
End With
'Datenbindungen
bs.DataSource = dt
lblSpalte0.DataBindings.Add _
(New Binding("Text", bs, "ID"))
txtSpalte1.DataBindings.Add _
(New Binding("Text", bs, "TextSpalte", False, _
DataSourceUpdateMode.OnPropertyChanged))
dtpSpalte2.DataBindings.Add _
(New Binding("Value", bs, "DateSpalte", False, _
DataSourceUpdateMode.OnPropertyChanged))
End Sub
Private Sub btnNext_Click(sender As Object, _
e As System.EventArgs) Handles btnNext.Click
With bs
If .Position = .Count - 1 Then .Position = 0 Else .Position += 1
End With
End Sub
Private Sub btnPrevious_Click(sender As Object, _
e As System.EventArgs) Handles btnPrevious.Click
With bs
If .Position = 0 Then .Position = .Count - 1 Else .Position -= 1
End With
End Sub
Private Sub btnAdd_Click(sender As Object, _
e As System.EventArgs) Handles btnAdd.Click
dt.Rows.Add(dt.Rows.Count + 1, "new", Today)
bs.Position = bs.Count - 1
End Sub
Private Sub btnSave_Click(sender As Object, _
e As System.EventArgs) Handles btnSave.Click
dt.WriteXml("C:\daten\testxyz.xml", True)
End Sub
End Class |