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 C1TrueDBGrid1 As C1.Win.C1TrueDBGrid.C1TrueDBGrid
    Friend WithEvents C1ExpressTable1 As C1.Data.Express.C1ExpressTable
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.C1TrueDBGrid1 = New C1.Win.C1TrueDBGrid.C1TrueDBGrid
        Me.C1ExpressTable1 = New C1.Data.Express.C1ExpressTable
        CType(Me.C1TrueDBGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.C1ExpressTable1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'C1TrueDBGrid1
        '
        Me.C1TrueDBGrid1.DataSource = Me.C1ExpressTable1
        Me.C1TrueDBGrid1.GroupByCaption = "Drag a column header here to group by that column"
        Me.C1TrueDBGrid1.Images.Add(CType(resources.GetObject("C1TrueDBGrid1.Images"), System.Drawing.Image))
        Me.C1TrueDBGrid1.Location = New System.Drawing.Point(24, 24)
        Me.C1TrueDBGrid1.Name = "C1TrueDBGrid1"
        Me.C1TrueDBGrid1.PreviewInfo.Location = New System.Drawing.Point(0, 0)
        Me.C1TrueDBGrid1.PreviewInfo.Size = New System.Drawing.Size(0, 0)
        Me.C1TrueDBGrid1.PreviewInfo.ZoomFactor = 75
        Me.C1TrueDBGrid1.PrintInfo.PageSettings = CType(resources.GetObject("C1TrueDBGrid1.PrintInfo.PageSettings"), System.Drawing.Printing.PageSettings)
        Me.C1TrueDBGrid1.Size = New System.Drawing.Size(560, 312)
        Me.C1TrueDBGrid1.TabIndex = 0
        Me.C1TrueDBGrid1.Text = "C1TrueDBGrid1"
        Me.C1TrueDBGrid1.PropBag = resources.GetString("C1TrueDBGrid1.PropBag")
        '
        'C1ExpressTable1
        '
        Me.C1ExpressTable1.BindingContextCtrl = Me
        Me.C1ExpressTable1.ConnectionComponent = Nothing
        Me.C1ExpressTable1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ComponentOne Studio" & _
            ".NET 2.0\common\Nwind.mdb;Persist Security Info=False"
        Me.C1ExpressTable1.DummyImage = CType(resources.GetObject("C1ExpressTable1.DummyImage"), System.Drawing.Bitmap)
        Me.C1ExpressTable1.FullComponentName = "Form1.C1ExpressTable1"
        Me.C1ExpressTable1.SchemaTableName = "Order Details"
        Me.C1ExpressTable1.TableViewName = "Order_Details"
        Me.C1ExpressTable1.SerializedSchemaXML = Me.C1ExpressTable1.GetSchemaFromResources(resources, "C1ExpressTable1", Me)
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(600, 357)
        Me.Controls.Add(Me.C1TrueDBGrid1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.C1TrueDBGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.C1ExpressTable1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub C1ExpressTable1_BeforeFieldChange(ByVal sender As Object, ByVal e As C1.Data.FieldChangeEventArgs) Handles C1ExpressTable1.BeforeFieldChange
        If e.Field.Name = "Quantity" Then
            If e.NewValue < 1 Then
                e.NewValue = 1
            End If
        ElseIf e.Field.Name = "Discount" Then
            If e.NewValue < 0 Or e.NewValue > 1 Then
                Throw New ApplicationException("Discount must be between 0 and 1")
            End If
        End If
    End Sub

    Private Sub C1ExpressTable1_BeforeEndEdit(ByVal sender As Object, ByVal e As C1.Data.RowChangeEventArgs) Handles C1ExpressTable1.BeforeEndEdit
        If e.Row("Quantity") * e.Row("UnitPrice") > 100000 Then
            Throw New ApplicationException("Too expensive")
        End If
    End Sub

#Region " Switch to C1NWind.mdb database "

    Private Sub C1ExpressTable1_BeforeFill(ByVal sender As System.Object, ByVal e As C1.Data.FillEventArgs) Handles C1ExpressTable1.BeforeFill
        If Not C1ExpressTable1.ExpressConnection.DataSet.Filled Then
            UpdateDbPath()
        End If
    End Sub

    Private Sub UpdateDbPath()
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If File.Exists(dbPath) Then
            Dim csb As New OleDbConnectionStringBuilder(C1ExpressTable1.ConnectionString)
            csb.DataSource = dbPath
            C1ExpressTable1.ConnectionString = csb.ToString()
        End If
    End Sub

#End Region

End Class
