using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

using C1.Data;
using C1.Data.SchemaObjects;

namespace ADOStorage
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private C1.Data.C1SchemaDef c1SchemaDef1;
        private C1.Data.C1DataSet c1DataSet1;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.DataGrid dataGrid2;
        private System.Windows.Forms.DataGrid dataGrid3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.CheckBox checkBox2;
        private System.Windows.Forms.CheckBox checkBox3;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.c1SchemaDef1 = new C1.Data.C1SchemaDef();
            this.c1DataSet1 = new C1.Data.C1DataSet();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.dataGrid2 = new System.Windows.Forms.DataGrid();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.dataGrid3 = new System.Windows.Forms.DataGrid();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox3 = new System.Windows.Forms.CheckBox();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).BeginInit();
            this.SuspendLayout();
            // 
            // c1SchemaDef1
            // 
            this.c1SchemaDef1.DataObjectsAssemblyFlags = ((C1.Data.DataObjectsAssemblyFlags)((C1.Data.DataObjectsAssemblyFlags.DataSetNamespaces | C1.Data.DataObjectsAssemblyFlags.StaticNameFields)));
            this.c1SchemaDef1.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("c1SchemaDef1.DummyImage")));
            this.c1SchemaDef1.CreateSchema += new System.EventHandler(this.c1SchemaDef1_CreateSchema);
            this.c1SchemaDef1.SerializedSchemaXML = this.c1SchemaDef1.GetSchemaFromResources(resources, "c1SchemaDef1", this);
            this.c1SchemaDef1.SerializedDesignTimeOptionsXML = this.c1SchemaDef1.GetDesignTimeOptionsFromResources(resources, "c1SchemaDef1", this);
            // 
            // c1DataSet1
            // 
            this.c1DataSet1.BindingContextCtrl = this;
            this.c1DataSet1.DataLibrary = "";
            this.c1DataSet1.DataLibraryUrl = "";
            this.c1DataSet1.DataSetDef = "DataSet";
            this.c1DataSet1.FillOnRequest = false;
            this.c1DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
            this.c1DataSet1.Name = "c1DataSet1";
            this.c1DataSet1.SchemaClassName = "";
            this.c1DataSet1.SchemaDef = this.c1SchemaDef1;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(16, 24);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(776, 560);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.groupBox1);
            this.tabPage1.Controls.Add(this.dataGrid2);
            this.tabPage1.Controls.Add(this.dataGrid1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(768, 534);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Simple tables";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioButton2);
            this.groupBox1.Controls.Add(this.radioButton1);
            this.groupBox1.Location = new System.Drawing.Point(248, 448);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(240, 80);
            this.groupBox1.TabIndex = 8;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Grids bound to";
            // 
            // radioButton2
            // 
            this.radioButton2.Location = new System.Drawing.Point(32, 56);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(160, 16);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "ADO.NET data set";
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            // 
            // radioButton1
            // 
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(32, 24);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(160, 24);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "C1DataObjects data set";
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // dataGrid2
            // 
            this.dataGrid2.CaptionText = "Orders";
            this.dataGrid2.DataMember = "";
            this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid2.Location = new System.Drawing.Point(8, 224);
            this.dataGrid2.Name = "dataGrid2";
            this.dataGrid2.Size = new System.Drawing.Size(752, 216);
            this.dataGrid2.TabIndex = 1;
            // 
            // dataGrid1
            // 
            this.dataGrid1.CaptionText = "Customers";
            this.dataGrid1.DataMember = "";
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 8);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(752, 216);
            this.dataGrid1.TabIndex = 0;
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.dataGrid3);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(768, 534);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Composite table";
            // 
            // dataGrid3
            // 
            this.dataGrid3.CaptionText = "CustomersOrders";
            this.dataGrid3.DataMember = "";
            this.dataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid3.Location = new System.Drawing.Point(8, 8);
            this.dataGrid3.Name = "dataGrid3";
            this.dataGrid3.Size = new System.Drawing.Size(752, 520);
            this.dataGrid3.TabIndex = 0;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(712, 592);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(120, 32);
            this.button2.TabIndex = 9;
            this.button2.Text = "Restore from XML";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(576, 592);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(120, 32);
            this.button1.TabIndex = 8;
            this.button1.Text = "Save to XML";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.Checked = true;
            this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBox1.Location = new System.Drawing.Point(24, 592);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(224, 24);
            this.checkBox1.TabIndex = 6;
            this.checkBox1.Text = "Filter rows in C1DataObjects data set";
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // checkBox2
            // 
            this.checkBox2.Checked = true;
            this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBox2.Location = new System.Drawing.Point(256, 592);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(128, 24);
            this.checkBox2.TabIndex = 10;
            this.checkBox2.Text = "Synchronize filtering";
            // 
            // checkBox3
            // 
            this.checkBox3.Location = new System.Drawing.Point(392, 592);
            this.checkBox3.Name = "checkBox3";
            this.checkBox3.Size = new System.Drawing.Size(168, 24);
            this.checkBox3.TabIndex = 11;
            this.checkBox3.Text = "Alternative synchronization";
            // 
            // saveFileDialog1
            // 
            this.saveFileDialog1.DefaultExt = "xml";
            this.saveFileDialog1.FileName = "adoDataSet";
            this.saveFileDialog1.Filter = "XML files|*.xml|All files|*.*";
            this.saveFileDialog1.Title = "Save ADO.NET data set as";
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.DefaultExt = "xml";
            this.openFileDialog1.Filter = "XML files|*.xml|All files|*.*";
            this.openFileDialog1.Title = "Restore ADO.NET data set from";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(840, 637);
            this.Controls.Add(this.checkBox3);
            this.Controls.Add(this.checkBox2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Activated += new System.EventHandler(this.Form1_Activated);
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private System.Data.DataSet adoDataSet = null;
        private bool isActivated = false;
        private bool adoBound = false;

        private void FillC1DataSet(bool filter)
        {
            // Initial data fetch from the database
            FilterConditions fcs = new FilterConditions();
            if (filter)
            {
                // If "Filter rows in C1DataObjects data set" is checked, use filter conditions
                FilterCondition fc = new FilterCondition(c1DataSet1, "Customers", "[CustomerID] < 'C'");
                fcs.Add(fc);
                fc = new FilterCondition(c1DataSet1, "Orders", "[CustomerID] < 'C'");
                fcs.Add(fc);
                fc = new FilterCondition(c1DataSet1, "CustomersOrders", "[CustomerID] >= 'C'");
                fcs.Add(fc);
            }
            c1DataSet1.Fill(fcs, false);
        }

        private void BindToC1DataSet()
        {
            // Bind all grids to the C1DataSet
            dataGrid1.DataSource = null;
            dataGrid1.DataSource = c1DataSet1;
            dataGrid1.DataMember = "Customers";
            dataGrid2.DataSource = null;
            dataGrid2.DataSource = c1DataSet1;
            dataGrid2.DataMember = "Orders";
            dataGrid3.DataSource = null;
            dataGrid3.DataSource = c1DataSet1;
            dataGrid3.DataMember = "CustomersOrders";
            MessageBox.Show(this, "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;
        }

        private void 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 = null;
            dataGrid1.DataSource = adoDataSet;
            dataGrid1.DataMember = "Customers";
            dataGrid2.DataSource = null;
            dataGrid2.DataSource = adoDataSet;
            dataGrid2.DataMember = "Orders";
            MessageBox.Show(this, "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;
        }

        private void 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();
        }

        private void 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 && checkBox2.Checked) // 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)
                //
                ArrayList fullList = c1DataSet1.GetDefaultTableViewRows("Customers"); // Get all rows of the Customers table
                // Filter them to determine which ones to include in the table view
                ArrayList filteredList = new ArrayList();
                foreach (C1DataRow row in fullList)
                    if (String.Compare(row["CustomerID"].ToString(), "C") < 0) // same filter condition as in FillC1DataSet()
                        filteredList.Add(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();
                foreach (C1DataRow row in fullList)
                    if (String.Compare(row["CustomerID"].ToString(), "C") < 0) // same filter condition as in FillC1DataSet()
                        filteredList.Add(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 (!checkBox3.Checked)
                {
                    // 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
                    foreach (IList 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.
                        C1DataRow rowCustomers = (C1DataRow)rows[0]; // This is a  Customers row
                        if (String.Compare(rowCustomers["CustomerID"].ToString(), "C") >= 0)  // same filter condition as in FillC1DataSet()
                            filteredList.Add(rows);
                    }
                }
                else
                {
                    // Second method: without using GetDefaultTableViewRows, collect the relevant table rows.
                    // In some cases, this can be faster than using GetDefaultTableViewRows.
                    C1DataTable dtCustomers = c1DataSet1.Tables["Customers"];
                    foreach (C1DataRow drCustomers in dtCustomers.Rows) // for each customer row
                        if (String.Compare(drCustomers["CustomerID"].ToString(), "C") >= 0) // 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
                            C1DataRow[] childRows = drCustomers.GetChildRows("Customers - Orders");
                            foreach (C1DataRow 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
                                C1DataRow[] composite = new C1DataRow[2];
                                composite[0] = drCustomers;
                                composite[1] = drOrders;
                                filteredList.Add(composite); // add the pair to the list
                            }
                        }
                }
                c1DataSet1.SetTableViewRows("CustomersOrders", filteredList, ""); // assign filtered row collection to the table view
            }

            c1DataSet1.StorageChangeEnd(); // Second stage complete, the process ends
        }

        private void Form1_Activated(object sender, System.EventArgs e)
        {
            if (!isActivated)
            {
                isActivated = true;
                FillC1DataSet(true);
                BindToC1DataSet();
                Focus();
            }
        }

        private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
        {
            if (!c1DataSet1.Filled)
                return;
            if (radioButton2.Checked && !adoBound)
            {
                FromC1ToADO();
                BindToADODataSet();
            }
        }

        private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
        {
            if (!c1DataSet1.Filled)
                return;
            if (radioButton1.Checked && adoBound)
            {
                FromADOToC1();
                BindToC1DataSet();
            }
        }

        private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
        {
            if (!c1DataSet1.Filled)
                return;
            FillC1DataSet(checkBox1.Checked);
            BindToC1DataSet();
            radioButton1.Checked = true;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                // 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.
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                // 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)
                        BindToADODataSet();
                    else
                    {
                        // Convert the simple table data to a C1DataSet, filling table views
                        // with pointers to table rows.
                        FromADOToC1();
                        BindToC1DataSet();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #region Switch to C1NWind.mdb database

        private void c1SchemaDef1_CreateSchema(object sender, EventArgs e)
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (File.Exists(dbPath))
            {
                Connection conn = c1SchemaDef1.Schema.Connections[0];
                OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder(conn.ConnectionString);
                csb.DataSource = dbPath;
                conn.ConnectionString = csb.ToString();
            }
        }

        #endregion
    }
}
