Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#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)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    '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 button2 As System.Windows.Forms.Button
    Friend WithEvents button1 As System.Windows.Forms.Button
    Friend WithEvents dataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents C1ExpressConnection1 As C1.Data.Express.C1ExpressConnection
    Friend WithEvents C1ExpressTable1 As C1.Data.Express.C1ExpressTable
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.button2 = New System.Windows.Forms.Button()
        Me.button1 = New System.Windows.Forms.Button()
        Me.dataGrid1 = New System.Windows.Forms.DataGrid()
        Me.C1ExpressConnection1 = New C1.Data.Express.C1ExpressConnection()
        Me.C1ExpressTable1 = New C1.Data.Express.C1ExpressTable()
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.C1ExpressConnection1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.C1ExpressTable1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'button2
        '
        Me.button2.Location = New System.Drawing.Point(460, 373)
        Me.button2.Name = "button2"
        Me.button2.Size = New System.Drawing.Size(104, 32)
        Me.button2.TabIndex = 8
        Me.button2.Text = "Bind to Categories"
        '
        'button1
        '
        Me.button1.Location = New System.Drawing.Point(244, 373)
        Me.button1.Name = "button1"
        Me.button1.Size = New System.Drawing.Size(104, 32)
        Me.button1.TabIndex = 7
        Me.button1.Text = "Bind to Customers"
        '
        'dataGrid1
        '
        Me.dataGrid1.DataMember = ""
        Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid1.Location = New System.Drawing.Point(60, 47)
        Me.dataGrid1.Name = "dataGrid1"
        Me.dataGrid1.Size = New System.Drawing.Size(696, 296)
        Me.dataGrid1.TabIndex = 6
        '
        'C1ExpressConnection1
        '
        Me.C1ExpressConnection1.BindingContextCtrl = Me
        Me.C1ExpressConnection1.ConnectionString = ""
        Me.C1ExpressConnection1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.C1ExpressConnection1.SerializedRelations = "<?xml version=""1.0""?><SerializedExpressRelations />"
        Me.C1ExpressConnection1.SerializedSchemaXML = Me.C1ExpressConnection1.GetSchemaFromResources(resources, "C1ExpressConnection1", Me)
        '
        'C1ExpressTable1
        '
        Me.C1ExpressTable1.BindingContextCtrl = Me
        Me.C1ExpressTable1.ConnectionComponent = Me.C1ExpressConnection1
        Me.C1ExpressTable1.ConnectionString = ""
        Me.C1ExpressTable1.FullComponentName = "Form1.C1ExpressTable1"
        Me.C1ExpressTable1.SchemaTableName = ""
        Me.C1ExpressTable1.TableViewName = "ExpressTable"
        Me.C1ExpressTable1.SerializedSchemaXML = Me.C1ExpressTable1.GetSchemaFromResources(resources, "C1ExpressTable1", Me)
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(816, 453)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button2, Me.button1, Me.dataGrid1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.C1ExpressConnection1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.C1ExpressTable1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub BindToCustomers()
        dataGrid1.DataSource = Nothing
        UpdateConnectionString()
        C1ExpressTable1.DbTableName = "Customers"
        C1ExpressConnection1.DataSet.Fill(True)
        dataGrid1.DataSource = C1ExpressTable1
        ' c1ExpressConnection1.DataSet.Fill(true) is necessary because we change the C1ExpressTable1's fields here, because of DbTableName.
        ' Without changing DbTableName, all we need to do would be
        '    c1ExpressConnection1.ConnectionString = ...
        '    c1ExpressConnection1.Fill()
    End Sub

    Private Sub BindToCategories()
        dataGrid1.DataSource = Nothing
        UpdateConnectionString()
        C1ExpressTable1.DbTableName = "Categories"
        C1ExpressConnection1.DataSet.Fill(True)
        dataGrid1.DataSource = C1ExpressTable1
        ' c1ExpressConnection1.DataSet.Fill(true) is necessary because we change the C1ExpressTable1's fields here, because of DbTableName.
        ' Without changing DbTableName, all we need to do would be
        '    c1ExpressConnection1.ConnectionString = ...
        '    c1ExpressConnection1.Fill()
    End Sub

    Private Sub UpdateConnectionString()
        Dim csb As New OleDbConnectionStringBuilder( _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ComponentOne Studio.NET 2.0\common\Nwind.mdb;Persist Security Info=False")

        ' Switch to C1NWind.mdb database
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If File.Exists(dbPath) Then
            csb.DataSource = dbPath
        End If

        C1ExpressConnection1.ConnectionString = csb.ToString()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindToCategories()
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        BindToCustomers()
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        BindToCategories()
    End Sub
End Class
