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 C1SchemaDef1 As C1.Data.C1SchemaDef
    Friend WithEvents C1DataSet1 As C1.Data.C1DataSet
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents checkBox3 As System.Windows.Forms.CheckBox
    Friend WithEvents checkBox2 As System.Windows.Forms.CheckBox
    Friend WithEvents button2 As System.Windows.Forms.Button
    Friend WithEvents button1 As System.Windows.Forms.Button
    Friend WithEvents checkBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents tabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents tabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents radioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents radioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents dataGrid2 As System.Windows.Forms.DataGrid
    Friend WithEvents dataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents tabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents dataGrid3 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.C1SchemaDef1 = New C1.Data.C1SchemaDef
        Me.C1DataSet1 = New C1.Data.C1DataSet
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.checkBox3 = New System.Windows.Forms.CheckBox
        Me.checkBox2 = New System.Windows.Forms.CheckBox
        Me.button2 = New System.Windows.Forms.Button
        Me.button1 = New System.Windows.Forms.Button
        Me.checkBox1 = New System.Windows.Forms.CheckBox
        Me.tabControl1 = New System.Windows.Forms.TabControl
        Me.tabPage1 = New System.Windows.Forms.TabPage
        Me.groupBox1 = New System.Windows.Forms.GroupBox
        Me.radioButton2 = New System.Windows.Forms.RadioButton
        Me.radioButton1 = New System.Windows.Forms.RadioButton
        Me.dataGrid2 = New System.Windows.Forms.DataGrid
        Me.dataGrid1 = New System.Windows.Forms.DataGrid
        Me.tabPage2 = New System.Windows.Forms.TabPage
        Me.dataGrid3 = New System.Windows.Forms.DataGrid
        CType(Me.C1SchemaDef1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.C1DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.tabControl1.SuspendLayout()
        Me.tabPage1.SuspendLayout()
        Me.groupBox1.SuspendLayout()
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.tabPage2.SuspendLayout()
        CType(Me.dataGrid3, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'C1SchemaDef1
        '
        Me.C1SchemaDef1.DataObjectsAssemblyFlags = CType((C1.Data.DataObjectsAssemblyFlags.DataSetNamespaces Or C1.Data.DataObjectsAssemblyFlags.StaticNameFields), C1.Data.DataObjectsAssemblyFlags)
        Me.C1SchemaDef1.DummyImage = CType(resources.GetObject("C1SchemaDef1.DummyImage"), System.Drawing.Bitmap)
        Me.C1SchemaDef1.SerializedSchemaXML = Me.C1SchemaDef1.GetSchemaFromResources(resources, "C1SchemaDef1", Me)
        Me.C1SchemaDef1.SerializedDesignTimeOptionsXML = Me.C1SchemaDef1.GetDesignTimeOptionsFromResources(resources, "C1SchemaDef1", Me)
        '
        'C1DataSet1
        '
        Me.C1DataSet1.BindingContextCtrl = Me
        Me.C1DataSet1.DataLibrary = ""
        Me.C1DataSet1.DataLibraryUrl = ""
        Me.C1DataSet1.DataSetDef = "DataSet"
        Me.C1DataSet1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.C1DataSet1.Name = "C1DataSet1"
        Me.C1DataSet1.SchemaClassName = ""
        Me.C1DataSet1.SchemaDef = Me.C1SchemaDef1
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "adoDataSet"
        Me.SaveFileDialog1.Filter = "XML files|*.xml|All files|*.*"
        Me.SaveFileDialog1.Title = "Save ADO.NET data set as"
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Filter = "XML files|*.xml|All files|*.*"
        Me.OpenFileDialog1.Title = "Restore ADO.NET data set from"
        '
        'checkBox3
        '
        Me.checkBox3.Location = New System.Drawing.Point(388, 586)
        Me.checkBox3.Name = "checkBox3"
        Me.checkBox3.Size = New System.Drawing.Size(168, 24)
        Me.checkBox3.TabIndex = 17
        Me.checkBox3.Text = "Alternative synchronization"
        '
        'checkBox2
        '
        Me.checkBox2.Checked = True
        Me.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked
        Me.checkBox2.Location = New System.Drawing.Point(252, 586)
        Me.checkBox2.Name = "checkBox2"
        Me.checkBox2.Size = New System.Drawing.Size(128, 24)
        Me.checkBox2.TabIndex = 16
        Me.checkBox2.Text = "Synchronize filtering"
        '
        'button2
        '
        Me.button2.Location = New System.Drawing.Point(708, 586)
        Me.button2.Name = "button2"
        Me.button2.Size = New System.Drawing.Size(120, 32)
        Me.button2.TabIndex = 15
        Me.button2.Text = "Restore from XML"
        '
        'button1
        '
        Me.button1.Location = New System.Drawing.Point(572, 586)
        Me.button1.Name = "button1"
        Me.button1.Size = New System.Drawing.Size(120, 32)
        Me.button1.TabIndex = 14
        Me.button1.Text = "Save to XML"
        '
        'checkBox1
        '
        Me.checkBox1.Checked = True
        Me.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked
        Me.checkBox1.Location = New System.Drawing.Point(20, 586)
        Me.checkBox1.Name = "checkBox1"
        Me.checkBox1.Size = New System.Drawing.Size(224, 24)
        Me.checkBox1.TabIndex = 13
        Me.checkBox1.Text = "Filter rows in C1DataObjects data set"
        '
        'tabControl1
        '
        Me.tabControl1.Controls.Add(Me.tabPage1)
        Me.tabControl1.Controls.Add(Me.tabPage2)
        Me.tabControl1.Location = New System.Drawing.Point(12, 18)
        Me.tabControl1.Name = "tabControl1"
        Me.tabControl1.SelectedIndex = 0
        Me.tabControl1.Size = New System.Drawing.Size(776, 560)
        Me.tabControl1.TabIndex = 12
        '
        'tabPage1
        '
        Me.tabPage1.Controls.Add(Me.groupBox1)
        Me.tabPage1.Controls.Add(Me.dataGrid2)
        Me.tabPage1.Controls.Add(Me.dataGrid1)
        Me.tabPage1.Location = New System.Drawing.Point(4, 22)
        Me.tabPage1.Name = "tabPage1"
        Me.tabPage1.Size = New System.Drawing.Size(768, 534)
        Me.tabPage1.TabIndex = 0
        Me.tabPage1.Text = "Simple tables"
        '
        'groupBox1
        '
        Me.groupBox1.Controls.Add(Me.radioButton2)
        Me.groupBox1.Controls.Add(Me.radioButton1)
        Me.groupBox1.Location = New System.Drawing.Point(248, 448)
        Me.groupBox1.Name = "groupBox1"
        Me.groupBox1.Size = New System.Drawing.Size(240, 80)
        Me.groupBox1.TabIndex = 8
        Me.groupBox1.TabStop = False
        Me.groupBox1.Text = "Grids bound to"
        '
        'radioButton2
        '
        Me.radioButton2.Location = New System.Drawing.Point(32, 56)
        Me.radioButton2.Name = "radioButton2"
        Me.radioButton2.Size = New System.Drawing.Size(160, 16)
        Me.radioButton2.TabIndex = 1
        Me.radioButton2.Text = "ADO.NET data set"
        '
        'radioButton1
        '
        Me.radioButton1.Checked = True
        Me.radioButton1.Location = New System.Drawing.Point(32, 24)
        Me.radioButton1.Name = "radioButton1"
        Me.radioButton1.Size = New System.Drawing.Size(160, 24)
        Me.radioButton1.TabIndex = 0
        Me.radioButton1.TabStop = True
        Me.radioButton1.Text = "C1DataObjects data set"
        '
        'dataGrid2
        '
        Me.dataGrid2.CaptionText = "Orders"
        Me.dataGrid2.DataMember = ""
        Me.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid2.Location = New System.Drawing.Point(8, 224)
        Me.dataGrid2.Name = "dataGrid2"
        Me.dataGrid2.Size = New System.Drawing.Size(752, 216)
        Me.dataGrid2.TabIndex = 1
        '
        'dataGrid1
        '
        Me.dataGrid1.CaptionText = "Customers"
        Me.dataGrid1.DataMember = ""
        Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid1.Location = New System.Drawing.Point(8, 8)
        Me.dataGrid1.Name = "dataGrid1"
        Me.dataGrid1.Size = New System.Drawing.Size(752, 216)
        Me.dataGrid1.TabIndex = 0
        '
        'tabPage2
        '
        Me.tabPage2.Controls.Add(Me.dataGrid3)
        Me.tabPage2.Location = New System.Drawing.Point(4, 22)
        Me.tabPage2.Name = "tabPage2"
        Me.tabPage2.Size = New System.Drawing.Size(768, 534)
        Me.tabPage2.TabIndex = 1
        Me.tabPage2.Text = "Composite table"
        Me.tabPage2.Visible = False
        '
        'dataGrid3
        '
        Me.dataGrid3.CaptionText = "CustomersOrders"
        Me.dataGrid3.DataMember = ""
        Me.dataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid3.Location = New System.Drawing.Point(8, 8)
        Me.dataGrid3.Name = "dataGrid3"
        Me.dataGrid3.Size = New System.Drawing.Size(752, 520)
        Me.dataGrid3.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(840, 637)
        Me.Controls.Add(Me.checkBox3)
        Me.Controls.Add(Me.checkBox2)
        Me.Controls.Add(Me.button2)
        Me.Controls.Add(Me.button1)
        Me.Controls.Add(Me.checkBox1)
        Me.Controls.Add(Me.tabControl1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.C1SchemaDef1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.C1DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.tabControl1.ResumeLayout(False)
        Me.tabPage1.ResumeLayout(False)
        Me.groupBox1.ResumeLayout(False)
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.tabPage2.ResumeLayout(False)
        CType(Me.dataGrid3, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private adoDataSet As System.Data.DataSet
    Private isActivated As Boolean = False
    Private adoBound As Boolean = False

    Private Sub FillC1DataSet(ByVal filter As Boolean)
        ' Initial data fetch from the database
        Dim fcs As C1.Data.FilterConditions = New C1.Data.FilterConditions()
        If filter Then
            ' If "Filter rows in C1DataObjects data set" is checked, use filter conditions
            Dim fc As C1.Data.FilterCondition = New C1.Data.FilterCondition(C1DataSet1, "Customers", "[CustomerID] < 'C'")
            fcs.Add(fc)
            fc = New C1.Data.FilterCondition(C1DataSet1, "Orders", "[CustomerID] < 'C'")
            fcs.Add(fc)
            fc = New C1.Data.FilterCondition(C1DataSet1, "CustomersOrders", "[CustomerID] >= 'C'")
            fcs.Add(fc)
        End If
        C1DataSet1.Fill(fcs, False)
    End Sub

    Private Sub BindToC1DataSet()
        ' Bind all grids to the C1DataSet
        dataGrid1.DataSource = Nothing
        dataGrid1.DataSource = C1DataSet1
        dataGrid1.DataMember = "Customers"
        dataGrid2.DataSource = Nothing
        dataGrid2.DataSource = C1DataSet1
        dataGrid2.DataMember = "Orders"
        dataGrid3.DataSource = Nothing
        dataGrid3.DataSource = C1DataSet1
        dataGrid3.DataMember = "CustomersOrders"
        MessageBox.Show(Me, "Bound to C1DataSet. Row counts in table views: Customers: " + C1DataSet1.TableViews("Customers").Rows.Count.ToString() + _
                " Orders: " + C1DataSet1.TableViews("Orders").Rows.Count.ToString() + " CustomersOrders: " + C1DataSet1.TableViews("CustomersOrders").Rows.Count.ToString())
        adoBound = False
    End Sub

    Private Sub BindToADODataSet()
        ' Bind two grids on the "Simple tables" tab to the ADO data set. The third grid,
        ' on the "Composite table" tab, shows a composite table having no analog in ADO.NET, so it is always
        ' bound to the C1DataObjects data set.
        dataGrid1.DataSource = Nothing
        dataGrid1.DataSource = adoDataSet
        dataGrid1.DataMember = "Customers"
        dataGrid2.DataSource = Nothing
        dataGrid2.DataSource = adoDataSet
        dataGrid2.DataMember = "Orders"
        MessageBox.Show(Me, "Bound to ADO.NET data set. Row counts in tables: Customers: " + adoDataSet.Tables("Customers").Rows.Count.ToString() + _
                " Orders: " + adoDataSet.Tables("Orders").Rows.Count.ToString())
        adoBound = True
    End Sub

    Private Sub FromC1ToADO()
        ' Converting from C1DataObjects data set to an ADO.NET data set is easy: simply make a copy of
        ' the underlying ADO.NET data set accessible through the StorageDataSet property.
        ' The resulting data set contains two tables, one for each C1DataObjects simple table, Orders and Customers.
        ' Composite tables have no analog in ADO.NET.
        adoDataSet = C1DataSet1.StorageDataSet.Copy()
    End Sub

    Private Sub FromADOToC1()
        ' Converting from ADO.NET data set to a C1DataSet (filling a C1DataSet with data from an ADO.NET data set)
        ' is a little trickier. The problem is that C1DataObjects has two-layer data model:
        ' there are simple tables, and then there are table views. Each table view is based on a simple or composite table.
        ' Simple table rows are stored in an ADO.NET data set (C1DataSet.StorageDataSet), but table view rows
        ' (which contain pointers to table rows) are stored internally by C1DataObjects, in a different place.
        ' They also need to be restored or created. Therefore, the conversion process has two stages:
        ' first, change C1DataSet.StorageDataSet (fill simple tables with data or modify their data as you need),
        ' second, fill table views with rows that point to table rows.
        ' Changing ADO.NET data tables comprising C1DataSet.StorageDataSet is only allowed on the first stage.
        ' When the first stage ends, tables are filled with data.
        ' At the beginning of the second stage, all table views are cleared so you can fill them with rows
        ' (pointing to table rows). To fill a table view, use the SetTableViewRows method. If you don't call
        ' SetTableViewRows for a table view, it will be called automatically at the end of the
        ' second stage, with default parameters. So, if your table view contains
        ' all possible table rows (this is the case here if checkBox1.Checked=False, in which case we don't
        ' do anything on the second stage). If you need to control
        ' the collection of table rows that belong to the table view, call SetTableViewRows.
        ' You have two options: either to enumerate the rows manually (for instance, scannning the existent
        ' table rows, finding the ones that must belong to the table view, see the code executing
        ' when checkBox2.Checked=True), or to call GetDefaultTableViewRows that provides you with the
        ' full list of all existing table rows, so you can filter them and then pass the filtered list
        ' to SetTableViewRows (this is done in the code below except the part executing on checkBox2.Checked=True).
        '
        ' So, let's get started. The two stages must be organized as follows:
        '
        ' StorageChangeBegin   ' start
        ' ...first stage...    ' fill or modify tables (modify C1DataSet.StorageDataSet)
        ' StorageChanged       ' first stage complete, tables filled, table views are undefined at this point
        ' ...second stage...   ' fill table views with rows pointing to tables (call SetTableViewRows)
        ' StorageChangeEnd     ' process complete, both tables and table views filled
        '
        C1DataSet1.StorageChangeBegin() ' start

        ' First stage
        '
        ' Copy data from adoDataSet to c1DataSet1.StorageDataSet
        C1DataSet1.StorageDataSet.Clear()
        C1DataSet1.StorageDataSet.Merge(adoDataSet)

        C1DataSet1.StorageChanged(True) ' first stage complete

        ' Second stage
        '
        If checkBox1.Checked And checkBox2.Checked Then ' If there are no filter conditions (so every
            ' table view contains all possible table rows, unrestricted),
            ' or if we don't want to synchronize filtering, we just skip the second stage, do nothing.
            ' There is no useful purpose in not synchronizing filtering, this case is only included
            ' for demonstration purposes. If we don't syncronize filtering (uncheck checkBox2),
            ' our table views will contain all customers and orders. To see that, uncheck the "Synchronize filtering" check box,
            ' check radioButton2, then check radioButton1 again. Message box will show that the number of rows grew. 
            '
            ' Control what rows you include in table views by calling SetTableViewRows
            '
            ' Fill the Customers table view (simple table view)
            '
            Dim fullList As ArrayList = C1DataSet1.GetDefaultTableViewRows("Customers") ' Get all rows of the Customers table
            ' Filter them to determine which ones to include in the table view
            Dim filteredList As ArrayList = New ArrayList()
            Dim row As C1.Data.C1DataRow
            For Each row In fullList
                If String.Compare(row("CustomerID").ToString(), "C") < 0 Then ' same filter condition as in FillC1DataSet()
                    filteredList.Add(row)
                End If
            Next row
            C1DataSet1.SetTableViewRows("Customers", filteredList, "") ' assign filtered row collection to the table view

            ' Fill the Orders table view (simple table view)
            fullList = C1DataSet1.GetDefaultTableViewRows("Orders") ' Get all rows of the Orders table
            ' Filter them to determine which ones to include in the table view
            filteredList = New ArrayList()
            For Each row In fullList
                If String.Compare(row("CustomerID").ToString(), "C") < 0 Then ' same filter condition as in FillC1DataSet()
                    filteredList.Add(row)
                End If
            Next row
            C1DataSet1.SetTableViewRows("Orders", filteredList, "") ' assign filtered row collection to the table view

            ' Fill the CustomerOrders table view (composite table view)
            filteredList = New ArrayList()
            ' We will show two alternative methods of filling a table view here. They both apply to simple table views as
            ' well as to composite ones, but the distinction is more important for composite table views because
            ' the first method (full scan of all possible rows) can be expensive for composite table views, since
            ' unrestricted joins can sometimes produce a very large number of potential rows.
            If Not checkBox3.Checked Then
                ' First method: scanning the full, unrestricted collection of possible rows, filtering it
                '
                ' Get the collection of all potential rows. In this case, they are all Customers-Orders rows joined
                ' by the CustomerID field (not too many in this case, about 800 rows in all, but in other cases it can be more)/
                fullList = C1DataSet1.GetDefaultTableViewRows("CustomersOrders") ' Get all composite rows
                Dim rows As IList
                For Each rows In fullList
                    ' Each composite row is a collection of C1DataRow objects, one for each "main" constituent table
                    ' ("main" means connected to its parent with a 1-M relation, not with an M-1 one).
                    ' In our case, each composite row contains two C1DataRow objects (rows(0), rows(1)),
                    ' the first is a Customers row, the second is an Orders row. Employees table,
                    ' although also belonging to the composite table, is not "main", because it is "linked"
                    ' to its parent, Orders with an M-1 relation. So a single Employees row can be uniqely
                    ' located for any given Orders row. That is why Employees rows do not participate here.
                    Dim rowCustomers As C1.Data.C1DataRow = CType(rows(0), C1.Data.C1DataRow)  ' This is a  Customers row
                    If String.Compare(rowCustomers("CustomerID").ToString(), "C") >= 0 Then ' same filter condition as in FillC1DataSet()
                        filteredList.Add(rows)
                    End If
                Next rows
            Else
                ' Second method: without using GetDefaultTableViewRows, collect the relevant table rows.
                ' In some cases, this can be faster than using GetDefaultTableViewRows.
                Dim dtCustomers As C1.Data.C1DataTable = C1DataSet1.Tables("Customers")
                Dim drCustomers As C1.Data.C1DataRow
                For Each drCustomers In dtCustomers.Rows ' for each customer row
                    If String.Compare(drCustomers("CustomerID").ToString(), "C") >= 0 Then ' check if it satisfies the filter condition (same as in FillC1DataSet())
                        ' For those that do not satisfy the condition, the nested loop is skipped
                        ' Get this customer's orders
                        Dim childRows As C1.Data.C1DataRow() = drCustomers.GetChildRows("Customers - Orders")
                        Dim drOrders As C1.Data.C1DataRow
                        For Each drOrders In childRows
                            ' For each order, create the composite "pointer" row, a pair of row objects, one is a customer, the other is an order
                            Dim composite As C1.Data.C1DataRow() = New C1.Data.C1DataRow(1) {drCustomers, drOrders}
                            filteredList.Add(composite) ' add the pair to the list
                        Next drOrders
                    End If
                Next drCustomers
            End If
            C1DataSet1.SetTableViewRows("CustomersOrders", filteredList, "") ' assign filtered row collection to the table view
        End If

        C1DataSet1.StorageChangeEnd() ' Second stage complete, the process ends
    End Sub

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        If Not isActivated Then
            isActivated = True
            FillC1DataSet(True)
            BindToC1DataSet()
            Focus()
        End If
    End Sub

    Private Sub radioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioButton2.CheckedChanged
        If radioButton2.Checked And Not adoBound Then
            FromC1ToADO()
            BindToADODataSet()
        End If

    End Sub

    Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioButton1.CheckedChanged
        If radioButton1.Checked And adoBound Then
            FromADOToC1()
            BindToC1DataSet()
        End If
    End Sub

    Private Sub checkBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles checkBox1.CheckedChanged
        If Not C1DataSet1.Filled Then Exit Sub
        FillC1DataSet(checkBox1.Checked)
        BindToC1DataSet()
        radioButton1.Checked = True
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
        If SaveFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            ' Save the ADO.NET data set in an XML file.
            ' It contains simple table data.
            C1DataSet1.StorageDataSet.WriteXml(SaveFileDialog1.FileName, XmlWriteMode.WriteSchema)
            ' Note that this is not the only way to save/restore a C1DataSet in XML.
            ' Saving/restoring it as an ADO.NET data set is used when you need to work with XML
            ' data in C1DataObjects, for example, to export data from C1DataObjects to XML to be processed by other means,
            ' or import XML data obtained from a non-C1DataSet source into a C1DataObjects data set.
            ' If you need to save a whole C1DataSet in XML only to restore it later into the C1DataSet
            ' component, it is much easier done using the C1DataSet.WriteXml/ReadXml methods.
        End If
    End Sub

    Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
        If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            ' Restore the ADO.NET data set, saved simple table data, in an XML file.
            Try
                adoDataSet = New System.Data.DataSet()
                adoDataSet.ReadXml(OpenFileDialog1.FileName, XmlReadMode.ReadSchema)
                If adoBound Then
                    BindToADODataSet()
                Else
                    ' Convert the simple table data to a C1DataSet, filling table views
                    ' with pointers to table rows.
                    FromADOToC1()
                    BindToC1DataSet()
                End If
            Catch ex As Exception
                MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

#Region " Switch to C1NWind.mdb database "

    Private Sub C1SchemaDef1_CreateSchema(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C1SchemaDef1.CreateSchema
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If File.Exists(dbPath) Then
            Dim conn As C1.Data.SchemaObjects.Connection = C1SchemaDef1.Schema.Connections(0)
            Dim csb As New OleDbConnectionStringBuilder(conn.ConnectionString)
            csb.DataSource = dbPath
            conn.ConnectionString = csb.ToString()
        End If
    End Sub

#End Region

End Class
