Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents iPanel As Microsoft.WindowsCE.Forms.InputPanel
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

    Protected Const cs = ""
    Private dataAdapter As SqlDataAdapter
    Private dataSet As DataSet
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnFlush As System.Windows.Forms.Button
    Friend WithEvents btnUpdate As System.Windows.Forms.Button
    Friend WithEvents txtAge As System.Windows.Forms.TextBox
    Friend WithEvents txtSurname As System.Windows.Forms.TextBox
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents btnLoad As System.Windows.Forms.Button
    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.iPanel = New Microsoft.WindowsCE.Forms.InputPanel
        Me.btnFlush = New System.Windows.Forms.Button
        Me.btnUpdate = New System.Windows.Forms.Button
        Me.txtAge = New System.Windows.Forms.TextBox
        Me.txtSurname = New System.Windows.Forms.TextBox
        Me.txtName = New System.Windows.Forms.TextBox
        Me.btnLoad = New System.Windows.Forms.Button
        '
        'DataGrid1
        '
        Me.DataGrid1.Location = New System.Drawing.Point(8, 88)
        Me.DataGrid1.Size = New System.Drawing.Size(216, 168)
        Me.DataGrid1.Text = "DataGrid1"
        '
        'btnFlush
        '
        Me.btnFlush.Location = New System.Drawing.Point(120, 56)
        Me.btnFlush.Size = New System.Drawing.Size(106, 20)
        Me.btnFlush.Text = "Flush"
        '
        'btnUpdate
        '
        Me.btnUpdate.Location = New System.Drawing.Point(120, 32)
        Me.btnUpdate.Size = New System.Drawing.Size(106, 20)
        Me.btnUpdate.Text = "Update"
        '
        'txtAge
        '
        Me.txtAge.Location = New System.Drawing.Point(8, 56)
        Me.txtAge.Text = "Age"
        '
        'txtSurname
        '
        Me.txtSurname.Location = New System.Drawing.Point(8, 32)
        Me.txtSurname.Text = "Surname"
        '
        'txtName
        '
        Me.txtName.Location = New System.Drawing.Point(8, 8)
        Me.txtName.Text = "Firstname"
        '
        'btnLoad
        '
        Me.btnLoad.Location = New System.Drawing.Point(120, 8)
        Me.btnLoad.Size = New System.Drawing.Size(107, 20)
        Me.btnLoad.Text = "Load"
        '
        'Form1
        '
        Me.Controls.Add(Me.btnFlush)
        Me.Controls.Add(Me.btnUpdate)
        Me.Controls.Add(Me.txtAge)
        Me.Controls.Add(Me.txtSurname)
        Me.Controls.Add(Me.txtName)
        Me.Controls.Add(Me.btnLoad)
        Me.Controls.Add(Me.DataGrid1)
        Me.MaximizeBox = False
        Me.Menu = Me.MainMenu1
        Me.MinimizeBox = False
        Me.Text = "Direct "

    End Sub

#End Region

    Private Sub txt_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.GotFocus, txtName.GotFocus, txtSurname.GotFocus
        iPanel.Enabled = True
    End Sub

    Private Sub txt_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.LostFocus, txtName.LostFocus, txtSurname.LostFocus
        iPanel.Enabled = False
    End Sub

    Private Sub BindText()
        txtName.DataBindings.Add("Text", dataSet.Tables(0), "Firstname")
        txtSurname.DataBindings.Add("Text", dataSet.Tables(0), "Surname")
        txtAge.DataBindings.Add("Text", dataSet.Tables(0), "Age")
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim cn As New SqlConnection("Password=saricky;User ID=sa;Initial Catalog=Demo;Data Source=FSDEVNET2")
        Dim commandBuilder As SqlCommandBuilder

        dataAdapter = New SqlDataAdapter("Select * from UgiUser", cn)
        commandBuilder = New SqlCommandBuilder(dataAdapter)

        dataSet = New DataSet
        dataAdapter.Fill(dataSet)
        DataGrid1.DataSource = dataSet.Tables(0)

        BindText()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim cm As CurrencyManager

        cm = CType(Me.BindingContext(dataSet.Tables(0)), CurrencyManager)
        cm.EndCurrentEdit()
    End Sub

    Private Sub btnFlush_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlush.Click
        Try
            dataAdapter.Update(dataSet)
            MessageBox.Show("Update complete")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

End Class
