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;

namespace CustomRelations
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.DataGrid dataGrid2;
        private C1.Data.C1SchemaDef c1SchemaDef1;
        private C1.Data.C1DataSet c1DataSet1;
        private C1.Data.C1DataSetLogic c1DataSetLogic1;
        private System.Windows.Forms.Button button1;
        /// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.c1DataSet1 = new C1.Data.C1DataSet();
            this.c1SchemaDef1 = new C1.Data.C1SchemaDef();
            this.dataGrid2 = new System.Windows.Forms.DataGrid();
            this.c1DataSetLogic1 = new C1.Data.C1DataSetLogic();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.DataMember = "_Orders";
            this.dataGrid1.DataSource = this.c1DataSet1;
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(16, 8);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(792, 240);
            this.dataGrid1.TabIndex = 0;
            // 
            // c1DataSet1
            // 
            this.c1DataSet1.BindingContextCtrl = this;
            this.c1DataSet1.DataLibrary = "";
            this.c1DataSet1.DataLibraryUrl = "";
            this.c1DataSet1.DataSetDef = "DataSet";
            this.c1DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
            this.c1DataSet1.Name = "c1DataSet1";
            this.c1DataSet1.SchemaClassName = "";
            this.c1DataSet1.SchemaDef = this.c1SchemaDef1;
            // 
            // c1SchemaDef1
            // 
            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);
            // 
            // dataGrid2
            // 
            this.dataGrid2.DataMember = "_Orders.Orders - Products";
            this.dataGrid2.DataSource = this.c1DataSet1;
            this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid2.Location = new System.Drawing.Point(16, 256);
            this.dataGrid2.Name = "dataGrid2";
            this.dataGrid2.Size = new System.Drawing.Size(792, 280);
            this.dataGrid2.TabIndex = 1;
            // 
            // c1DataSetLogic1
            // 
            this.c1DataSetLogic1.DataSetDef = "DataSet";
            this.c1DataSetLogic1.SchemaComponent = this.c1SchemaDef1;
            this.c1DataSetLogic1.SchemaPersist = null;
            this.c1DataSetLogic1.GetChildRows += new C1.Data.GetListEventHandler(this.c1DataSetLogic1_GetChildRows);
            this.c1DataSetLogic1.GetParentRow += new C1.Data.GetListEventHandler(this.c1DataSetLogic1_GetParentRow);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(368, 560);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(80, 32);
            this.button1.TabIndex = 2;
            this.button1.Text = "Show parent";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(832, 605);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.dataGrid2);
            this.Controls.Add(this.dataGrid1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).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 void c1DataSetLogic1_GetChildRows(object sender, C1.Data.GetListEventArgs e)
        {
            ArrayList list = new ArrayList();
            if (e.RelationName == "Orders - Products")
            {
                foreach (C1DataRow childOrderDetails in e.Row.GetChildRows("Orders - Order Details"))
                    foreach (C1DataRow childProducts in childOrderDetails.GetChildRows("Products - Order Details"))
                        if (!list.Contains(childProducts))
                            list.Add(childProducts);
            }
            e.List = list;
        }

        private void c1DataSetLogic1_GetParentRow(object sender, C1.Data.GetListEventArgs e)
        {
            if (e.RelationName == "Orders - Products")
            {
                C1DataRow parentOrderDetails = e.Row.GetParentRow("Products - Order Details");
                if (parentOrderDetails != null)
                {
                    C1DataRow parentOrders = parentOrderDetails.GetParentRow("Orders - Order Details");
                    e.List.Add(parentOrders);
                }
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            CurrencyManager cm = (CurrencyManager)BindingContext[c1DataSet1, "_Orders.Orders - Products"];
            C1DataRow r = C1DataRow.FromDataItem(cm.Current).GetParentRow("Orders - Products");
            MessageBox.Show(r["CustomerID"].ToString());
        }

        #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))
            {
                C1.Data.SchemaObjects.Connection conn = c1SchemaDef1.Schema.Connections[0];
                OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder(conn.ConnectionString);
                csb.DataSource = dbPath;
                conn.ConnectionString = csb.ToString();
            }
        }

        #endregion
    }
}
